我是一个C#程序员,在VS 2008中的C++项目上工作,试图创建一个对象的实例并将其作为参数传递给构造函数。当我这样做时,我正在接收链接器错误,而我正在努力诊断。C++字符串传递给类的构造函数 - 链接器错误
我得到的链接错误是
2>TestMyProj.obj : error LNK2028: unresolved token (0A0002B9) "public: __thiscall myNamespace::myClass::myClass(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" ([email protected]@@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) referenced in function "int __cdecl main(void)" ([email protected]@$$HYAHXZ)
2>TestMyProj.obj : error LNK2019: unresolved external symbol "public: __thiscall myNamespace::myClass::myClass(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" ([email protected]@@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) referenced in function "int __cdecl main(void)" ([email protected]@$$HYAHXZ)
2>..\MyProj\TestMyProj.dll : fatal error LNK1120: 2 unresolved externals
从中试图创建类的实例项目(TestMyProj)。 MyProj编译得很好。代码如下:
MyProj.cpp
#include "MyProj.h"
namespace myNamespace
{
myClass::myClass(string inString){}
};
MyProj.h:
#pragma once
#include <string>
using namespace std;
namespace myNamespace
{
class myClass
{
public:
myClass::myClass(string inString);
};
}
在那里我试图创建MyClass类的实例代码是在另一个项目
#include <string>
#include "../MyProj/MyProj.h"
int main()
{
myNamespace::myClass("");
return 0;
}
0:相同的溶液
TestMyProj.cpp内
我显然误解了一些基本的东西,可能是关于头文件的性质。我在很大程度上是以前编码的例子。
有人可以指出我做错了什么吗?
https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-doi-i-fix – CoryKramer
此外,由于您使用的是类在一个它定义的项目以外的项目中,你可能需要导出它https://stackoverflow.com/questions/6840576/how-to-export-ac-class-from-a-dll – CoryKramer
1.你有一个半 - 在MyProj.cpp中的命名空间的结尾处,2.在MyProj.h中,在类myClass括号内具有限定符“myClass ::”。 –