获取"error LNK2019: unresolved external symbol ... "

Getting "error LNK2019: unresolved external symbol ... "

本文关键字:external symbol unresolved LNK2019 error 获取      更新时间:2023-10-16

我正在尝试开始使用C++但我不断收到此错误。我知道我的代码的哪些部分正在生成它,但我认为至少有一个这些部分不应该生成它们。

我正在创建一个名为 Text 的类,它的运行方式类似于 std::string 类,只是为了试验和更好地理解值语义。

无论如何,这些是我的文件:

文本.h:

#ifndef TEXT
#define TEXT
class Text {
   public:
      Text(const char *str);
      Text(const Text& other);
      void operator=(const Text& other);
      ~Text();
   private:
      int size;
      char* cptr;           
};
#endif

文本.cpp:

#include "Text.h"
#include <cstring>
#include <iostream>
using namespace std;
Text::Text(const char* str) {
    size = strlen(str) + 1;
    cptr = new char[size];
    strcpy(cptr, str);
}
Text::Text(const Text& other) {
    size = other.size;
    cptr = new char[size];
    strcpy(cptr, str);
}
void Text::operator=(const Text& other){
    delete [] cptr;
    size = other.size;
    cptr = new char[size];
    strcpy(cptr, other.ctpr);
}
Text::~Text() {
    delete [] cptr;
}

主.cpp:

#include <iostream>
#include "Text.h"
using namespace std;
Text funk(Text t) {
    // ...
    return t;
}
int main() {
    Text name("Mark");
    Text name2("Knopfler");
    name = funk(name);
    name = name2;
    return 0;
}

因此,导致错误的是函数funk,以及main函数中的前两行。我明白为什么它在主函数的前两行抱怨,因为没有名为"name"或"name2"的函数。但是我想做的是在一行中声明和初始化一个对象(我和老 Java 家伙:p),这在C++中甚至可能吗?我在网上找不到任何表明这一点的东西。

有趣的是,这段代码或多或少是从我的讲师在讲座中执行的一些代码中复制的。他当然也没有声明任何名为"name"和"name2"的函数。对此有什么合理的解释吗?

但是为什么函数funk生成此错误呢?我所做的只是返回我发送的对象的副本。

提前感谢!

编辑:这是完整的错误消息。其中有五个。"第二申请"是我的项目名称。

错误

1 错误 LNK2019:未解析的外部符号"公共:__thiscall文本::文本(字符常量 *)"(??0Text@@QAE@PBD@Z) 在函数 _main C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication 中引用


错误

2 错误 LNK2019:未解析的外部符号"公共:__thiscall文本::文本(类文本常量 &)"(??0Text@@QAE@ABV0@@Z)在函数"类文本__cdecl放克(类文本)"中引用(?funk@@YA?AVText@@V1@@Z) C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication


错误

3 错误 LNK2019:未解析的外部符号"public: void __thiscall Text::operator=(class Text const &)"(??4Text@@QAEXABV0@@Z) 在函数 _main C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication 中引用


错误

4 错误 LNK2019:未解析的外部符号"公共:__thiscall文本::~文本(无效)"(??1Text@@QAE@XZ)在函数"类文本__cdecl放克(类文本)"中引用(?funk@@YA?AVText@@V1@@Z) C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication


错误

5 错误 LNK1120:4 个未解析的外部 C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\Debug\SecondApplication.exe 1 1 secondApplication

例如

,如果您忘记将"Text.cpp"添加到项目中,则会看到链接错误(不是编译错误),这样它就不会被编译和链接。

代码中有两个错误 - 一个在复制构造函数中,一个在赋值运算符中。

由于编译器没有抱怨这两个错误,我怀疑您忘记将文件添加到项目中。