[英]VC++ 2010 error LNK2019: unresolved external symbol
main cpp file: 主cpp文件:
#include <iostream>
#include <cstdio>
#include "table.h"
using namespace std;
int main() {
Table test;
int i;
for(i = 0; i < 26; i++) {
cout << test.start[2] << endl;
}
system("PAUSE");
return 0;
}
header file: 头文件:
#pragma once
class Table {
public:
char start[26];
Table();
Table(char key[26]);
~Table();
};
cpp file: cpp文件:
#include "table.h"
Table::Table() {
char start[26] = "ABCDEFGHIJKLMNOPRSTUVWXYZ";
}
Table::Table(char key[26]) {
}
errors im getting : 错误即时通讯:
1>playfair.obj : error LNK2019: unresolved external symbol "public: __thiscall Table::~Table(void)" (??1Table@@QAE@XZ) referenced in function _main
1>c:\Users\Jansu\Documents\Visual Studio 2010\Projects\playfair\Debug\playfair.exe : fatal error LNK1120: 1 unresolved externals
so basically i googled a lot and do not know what to do. 所以基本上我用谷歌搜索很多,不知道该怎么办。 i found some answers but i tried them and did not help
我找到了一些答案,但我尝试了但没有帮助
for example i tried to add additional dependencies , but i had all of them already added. 例如,我尝试添加其他依赖项 ,但是我已经添加了所有依赖项 。
help me please, why does the error come? 请帮助我,为什么会出现错误?
You have to define the destructor in your cpp file : 您必须在cpp文件中定义析构函数:
Table::~Table()
{
}
Although the header defines Table
as having a dtor, the cpp file only contains a couple of constructors -- not a destructor. 尽管标头将
Table
定义为具有dtor,但cpp文件仅包含几个构造函数-不含析构函数。 Given that there seems to be nothing for your destructor to do (you haven't allocated any dynamic memory or anything like that) you probably just want to remove the declaration of Table::~Table();
鉴于您的析构函数似乎无事可做(您尚未分配任何动态内存或类似的东西),您可能只想删除
Table::~Table();
的声明Table::~Table();
and be done with it. 并完成它。 While you're at it, you probably want to make
Table::start
private. 在使用它时,您可能希望将
Table::start
设为私有。 I'd also change the parameter to a char const *
instead of using array notation: 我还将参数更改为
char const *
而不是使用数组符号:
class Table {
char start[26];
public:
Table();
Table(char const *key);
};
Once you're done dealing with that, you'll need to deal with the fact that Table::Table()
defines a local variable named start
, and initializes it, but leaves Table::start
uninitialized, which I doubt is what you wanted/intended. 处理完之后,您将需要处理
Table::Table()
定义一个名为start
的局部变量并对其进行初始化的事实,但是将Table::start
保留为未初始化的事实,我怀疑这是您的实际情况。想要/打算。
unresolved external symbol "public: __thiscall Table::~Table(void)" (??1Table@@QAE@XZ) referenced in function _main
函数_main中引用的未解析的外部符号“ public:__thiscall Table ::〜Table(void)”(?? 1Table @@ QAE @ XZ)
It means exactly what it says. 这就是说的意思。
"unresolved" = "couldn't be found". “ unresolved” =“无法找到”。 "external symbol" = "definition of the function".
“外部符号” =“功能的定义”。 It is looking for the destructor - the important part is "Table::~Table".
它正在寻找析构函数-重要的部分是“ Table ::〜Table”。
Your class definition refers to a destructor, but doesn't implement it. 您的类定义引用了一个析构函数,但是没有实现它。 Neither does the implementation file.
实施文件也没有。 Because you referred to a destructor, the compiler will not make the usual automatic do-nothing one for you.
因为您引用的是析构函数,所以编译器不会为您执行通常的自动禁用操作。 The destructor is called in main() because you create an instance in main(), and the destructor is needed to clean it up at the end of main().
在main()中调用析构函数,因为您在main()中创建了一个实例,并且需要析构函数在main()的末尾清理它。
BTW, your constructor does not actually initialize the data in the table. 顺便说一句,您的构造函数实际上并未初始化表中的数据。 It creates a local array named
start
which is then promptly thrown away, leaving the member alone. 它创建一个名为
start
的本地数组,然后立即将其丢弃,使成员保持独立。
You won't be able to just assign from the string to the array member, either. 您也将不能只从字符串将其分配给数组成员。 You'll need to use a copying function, such as std::copy:
您需要使用复制功能,例如std :: copy:
Table::Table() {
char* alphabet = "ABCDEFGHIJKLMNOPRSTUVWXYZ";
std::copy(alphabet, alphabet + 26, start); // std::copy comes from <algorithm>.
}
You haven't defined the destructor, only declared it. 您尚未定义析构函数,仅对其进行了声明。 Try changing the header to this:
尝试将标头更改为此:
#pragma once
class Table {
public:
char start[26];
Table();
Table(char key[26]);
~Table() {}
};
You declared the destructor, so you must define it, too. 您声明了析构函数,因此也必须定义它。
The hint's in that the linker error refers to the destructor. 提示在于链接器错误引用了析构函数。
And Jerry's right about your array initialisation issue: you in fact do not initialise that array at all, but create some local one in the constructor body that never gets used. 杰里(Jerry)对您的数组初始化问题是正确的:实际上,您根本不初始化该数组,而是在构造函数主体中创建一些从未使用过的本地数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.