LNK2019 使用非模板类

发布于 2024-11-15 23:15:59 字数 1328 浏览 14 评论 0原文

当使用引用其中其他类的类时,我遇到链接器错误。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

药祭#氼 2024-11-22 23:15:59

您尚未定义默认构造函数和析构函数,如下所示:

MovePattern();  //default constructor
~MovePattern(); //destructor

如果声明它们,则必须定义它们。声明必须有定义。否则,在使用它们时,无论是隐式的还是显式的,您都会遇到链接器错误。

You've not defined the default constructor and the destructor, as listed below:

MovePattern();  //default constructor
~MovePattern(); //destructor

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.

热情消退 2024-11-22 23:15:59

此链接器错误是因为您尚未编译/链接包含默认构造函数 (MovePattern::MovePattern()) 和析构函数 (MovePattern::~MovePattern()< /代码>) 定义。

您应该在 类 MovePattern 中声明它们内联(如果您实际上没有在其中做太多事情):

class MovePattern {
public:
  MovePattern () {}
  ~MovePattern () {}
};

或者在单独的 .cpp 文件中定义它们并编译/链接该文件与您的来源:

// MovePattern.cpp
MovePattern::MovePattern ()
{
  //...
}
MovePattern::~MovePattern ()
{
  //...
}

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 the class MovePattern (if you don't really do much in it):

class MovePattern {
public:
  MovePattern () {}
  ~MovePattern () {}
};

Or define them in a seperate .cpp file and compile/link that file with your source:

// MovePattern.cpp
MovePattern::MovePattern ()
{
  //...
}
MovePattern::~MovePattern ()
{
  //...
}
ゞ记忆︶ㄣ 2024-11-22 23:15:59

只需删除这两个声明:

MovePattern();
~MovePattern();

您显然不需要它们——因为您在单个 cpp 文件中定义了所有内容。

Just remove these two declarations:

MovePattern();
~MovePattern();

You obviously do not need these -- since you defined everything in a single cpp file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文