我有一个编译问题。
- 我有一个类
- 我有一个头文件
- 当然还有 Main 来测试我的工作。
但是我遇到了编译错误,这是我不理解我做错了什么。
头文件:
#ifndef AGENT_H
#define AGENT_H
using namespace std;
class Agent
{
public:
Agent(string);
virtual ~Agent();
private:
string name;
};
#endif /* AGENT_H */
代理类(Agent.cpp)
#include "Agent.h"
using namespace std;
Agent::Agent(string _name)
{
this->name = _name;
}
Agent::~Agent()
{
delete this->name;
}
还有我的主要:
#include <cstdlib>
#include <iostream>
#include "Agent.h"
using namespace std;
int main(int argc, char** argv)
{
Agent agent1("Danila");
return 0;
}
所以我得到了这样奇怪的错误:
undefined reference to `Agent::Agent(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/main.cpp:17: undefined reference to `Agent::~Agent()'
/main.cpp:17: undefined reference to `Agent::~Agent()'
你们能帮我看看那里出了什么问题吗?
最佳答案
你需要一个 #include <string>
在你的头文件中。
此外,为了良好实践,请保留 using namespace
s 在你的 .cpp 文件中,如果有的话。
关于C++ 简单编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9251751/