c++ - 错误 LNK2019 : unresolved external symbol (simple code)

标签 c++

这是一个非常简单的代码,即使我编写它的方式与其他人执行相同代码的方式相同,也会出现错误

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 .拼写很重要。还有:

  1. 为什么要在 .h 文件中定义函数?只需在那里声明它们并在 .cpp 文件中定义它们(除非它们是模板)。
  2. 不要输入using namespace <whatever>在头文件中。您只是在弄乱包含 header 的任何内容的全局命名空间。
  3. 您不需要将该函数标记为 static .

关于c++ - 错误 LNK2019 : unresolved external symbol (simple code),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15015779/

相关文章:

c++ - 标准库是否提供检查 double 是否与整数相同的函数?

c++ - 套接字编程学习

c++ - 在运行时加载 opengl/directx API 函数

c++ - 是否可以在不使用尾随返回类型语法的情况下通过 lambda 引用返回类型为 T 的对象?

c++ - 多线程 - 在数组中我应该保护什么?

c++ - Win32 文件 IO 工作但 fopen 等。阿尔。失败。这里发生了什么?

c++ - 无法访问公共(public)静态方法

c++ - 使用 new 关键字和不使用 new 关键字在 C++ 中创建对象的区别

c++ - 为什么执行 socket::readyRead() 的新信号,即使其较早的插槽仍在处理中?

c++ - 通过父类(super class)的引用访问派生类