C++ 简单编译错误

标签 c++ compilation header

我有一个编译问题。

  • 我有一个类
  • 我有一个头文件
  • 当然还有 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/

相关文章:

c++ - 如何重载 << 运算符?

module - 从多个项目导入 D 模块

c++ - 如何从多个 cpp 文件编译 WebAssembly?

android - StickyGridHeader 示例

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

c++ - 辅音计数程序不返回辅音数

c++ - C++编译器在编译 `operator delete`时如何工作?

c - 是否可以检测程序是否已通过/proc/self/mem 动态链接?

PHP header 重定向不会在 IE 中重新加载 &lt;iframe&gt;

c++ - mmorpg中的c多线程