1
votes

this is a pretty simple code that just is coming up with an error even though I have it written the same way other people doing the same code have it

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

the .cpp file

#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;
}   

the .h file

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

static string promptFromString(string prompt)
{
string filename;
cout << prompt;
cin >> filename;
return filename;
}  
3

3 Answers

3
votes

You never define prompt**For**String, you defined prompt**From**String. Spelling matters. Also:

  1. Why are you defining functions in your .h file? Just declare them there and define them in the .cpp file (unless they're templates).
  2. Don't put using namespace <whatever> in a header file. You're just mucking up the global namespace of whatever includes your header.
  3. You don't need to mark that function as static.
0
votes

This line:

string promptForString(string prompt);

In your .cpp file is causing issues. It's forward delcaring a function with external linkage. However, the function your header is:

static string promptFromString(string prompt)
{
...

The important part here is the static. static means it has internal linkage. Either get rid of the static, or get rid of the forward declaration, because a function can't have both internal and external linkage.

Edit: also, Ed S. made a good find with your typo.

0
votes

you call promptForString() from your main function while you have promptFromString() defined in .h file.

You might want to change one of the definitions.