这是一个非常简单的代码,即使我编写它的方式与其他人执行相同代码的方式相同,也会出现错误
1>assigntment5.obj : error LNK2019: unresolved external symbol "class std::basic_string,class std::allocator > __cdecl promptForString(class std::basic_string,class std::allocator >)" (?promptForString@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@Z) referenced in function _main 1>c:\users\aweb\documents\visual studio 2010\Projects\Assignment5\Debug\Assignment5.exe : fatal error LNK1120: 1 unresolved externals
.cpp 文件
#include <iostream>
#include <string>
#include "anw65_Library.h"
using namespace std;
string promptForString(string prompt);
int main()
{
string name = promptForString("What is the filename?: ");
system("pause");
return 0;
}
.h 文件
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
static string promptFromString(string prompt)
{
string filename;
cout << prompt;
cin >> filename;
return filename;
}
最佳答案
你永远不会定义 prompt**For**String
, 你定义了 prompt**From**String
.拼写很重要。还有:
- 为什么要在 .h 文件中定义函数?只需在那里声明它们并在 .cpp 文件中定义它们(除非它们是模板)。
- 不要输入
using namespace <whatever>
在头文件中。您只是在弄乱包含 header 的任何内容的全局命名空间。 - 您不需要将该函数标记为
static
.
关于c++ - 错误 LNK2019 : unresolved external symbol (simple code),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15015779/