LNK2019 使用非模板类
当使用引用其中其他类的类时,我遇到链接器错误。
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MovePattern::~MovePattern(void)" (??1MovePattern@@QAE@XZ) referenced in function "public: __thiscall Enemy::Enemy(int,int,class MovePattern,char)" (??0Enemy@@QAE@HHVMovePattern@@D@Z)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MovePattern::MovePattern(void)" (??0MovePattern@@QAE@XZ) referenced in function "public: __thiscall Enemy::Enemy(int,int,class MovePattern,char)" (??0Enemy@@QAE@HHVMovePattern@@D@Z)
它来自于引用此类:
class MovePattern{
public:
char next;
MovePattern();
MovePattern(const MovePattern &old){
p = old.p;
pi = 0;
next = p[0];
n = p[0];
}
MovePattern(char *pattern){
p = pattern;
pi = 0;
next = p[0];
n = p[0];
}
~MovePattern();
在此类中:
class Enemy{
public:
Enemy(int a, int b, MovePattern p,char c)
x = b;
y = a;
MovePattern pattern (p);
symbol = c;
它们当前位于同一个 .cpp 文件中,并且 MovePattern 位于 Enemy 之上。
我不确定
带有空白控制台项目的 Visual C++ 2010 Express 发生了什么,而且我还没有找到与我的问题类似的任何内容,任何帮助将不胜感激。
I'm getting linker errors when using Classes that reference other classes in them.
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MovePattern::~MovePattern(void)" (??1MovePattern@@QAE@XZ) referenced in function "public: __thiscall Enemy::Enemy(int,int,class MovePattern,char)" (??0Enemy@@QAE@HHVMovePattern@@D@Z)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MovePattern::MovePattern(void)" (??0MovePattern@@QAE@XZ) referenced in function "public: __thiscall Enemy::Enemy(int,int,class MovePattern,char)" (??0Enemy@@QAE@HHVMovePattern@@D@Z)
It's from referencing this class:
class MovePattern{
public:
char next;
MovePattern();
MovePattern(const MovePattern &old){
p = old.p;
pi = 0;
next = p[0];
n = p[0];
}
MovePattern(char *pattern){
p = pattern;
pi = 0;
next = p[0];
n = p[0];
}
~MovePattern();
In this class:
class Enemy{
public:
Enemy(int a, int b, MovePattern p,char c)
x = b;
y = a;
MovePattern pattern (p);
symbol = c;
They are currently within the same .cpp file, and MovePattern is above Enemy.
I'm not sure what is going on here
Visual C++ 2010 Express with a blank console project, and I haven't found anything similar to my problem yet, any help would would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尚未定义默认构造函数和析构函数,如下所示:
如果声明它们,则必须定义它们。声明必须有定义。否则,在使用它们时,无论是隐式的还是显式的,您都会遇到链接器错误。
You've not defined the default constructor and the destructor, as listed below:
You've to define them IF you declare them. Declaration must have definition. Or else you'll get linker error when using them, either implicitly or explicitly.
此链接器错误是因为您尚未编译/链接包含默认构造函数 (
MovePattern::MovePattern()
) 和析构函数 (MovePattern::~MovePattern()< /代码>) 定义。
您应该在
类 MovePattern
中声明它们内联
(如果您实际上没有在其中做太多事情):或者在单独的 .cpp 文件中定义它们并编译/链接该文件与您的来源:
This linker error is because you haven't compiled/linked the file/code which contains the default constructor (
MovePattern::MovePattern()
) and destructor(MovePattern::~MovePattern()
) definitions.You should either declare them
inline
inside theclass MovePattern
(if you don't really do much in it):Or define them in a seperate .cpp file and compile/link that file with your source:
只需删除这两个声明:
您显然不需要它们——因为您在单个 cpp 文件中定义了所有内容。
Just remove these two declarations:
You obviously do not need these -- since you defined everything in a single cpp file.