Aar*_*ron 3 c++ undefined-reference
我不太熟悉c ++以及实例化对象是如何工作的,所以这可能是一件非常简单的事情.当我用g ++编译时,我得到错误"未定义引用'Foo :: Foo(std :: string)'".我想创建一个类Foo的实例,它的构造函数中有一个字符串参数.这是代码:
#include <string>
using namespace std;
class Foo
{
public:
Foo(string s);
private:
string id;
};
Run Code Online (Sandbox Code Playgroud)
#include <string>
#include "Foo.h"
using namespace std;
Foo::Foo(string s)
{
id = s;
}
Run Code Online (Sandbox Code Playgroud)
#include <string>
#include "Foo.h"
using namespace std;
int main()
{
Foo foo("bar");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Fre*_*son 15
您可能没有在编译行中包含Foo.cpp.它应该看起来像这样:
g++ main.cpp Foo.cpp -o testFoo
Run Code Online (Sandbox Code Playgroud)