0

I'm getting an error:

1> Source.cpp 1>Source.obj : error LNK2019: unresolved external symbol "void __cdecl saveToFile(char const * const,struct Task * const,int)" (?saveToFile@@YAXQBDQAUTask@@H@Z) referenced in function _main 1>C:\Users\Evan\Desktop\Coding Stuff\C++ programs\CS162 HW\cs_162_hw_2\Debug\cs_162_hw_2.exe : fatal error LNK1120: 1 unresolved externals

and it only happens when i attempt to run my saveToFile function. Here is the code for that function:

void saveToFile(const char fileName[MAX_CHAR], const Task list[], int size)
{
ofstream out;

out.open(fileName);
if(!out)
{
    cerr << "Fail to open " << fileName << " for writing!" << endl;
    exit(1);
}

int index;
for(index=0; index < size; index++)
{
    out << list[index].course << ';' << list[index].description << ';' <<       list[index].date << endl;
}

out.close();
 }
ollo
  • 24,797
  • 14
  • 106
  • 155
chillpenguin
  • 2,989
  • 2
  • 15
  • 18
  • And the source file this function is in, is actually included in your project? Is it compiled and linked with the other files? – Some programmer dude Apr 28 '13 at 05:37
  • 1
    Is saveToFile part of a class or a standalone function? – billz Apr 28 '13 at 05:38
  • I suggest you rewrite function signature to: `void saveToFile(const std::string& fileName, const std::vector& task_list)` – billz Apr 28 '13 at 05:39
  • @billz Then he will get compiler errors where his other code tries to call `saveToFile`... I don't think you are helping. – Matt Phillips Apr 28 '13 at 05:41
  • Are you using VS? You need to add the .cpp file which contains this code to your project, there is a straightforward command for that I think. – Matt Phillips Apr 28 '13 at 05:44
  • @MattPhillips won't much to change at all. Mixing C/C++ always makes code harder to maintain than simply change to better pure c++ code. – billz Apr 28 '13 at 05:44
  • All my functions are in my 1 and only source code file for this project. Everything runs fine if i comment out the saveToFile function. I have a function that reads the data from the txt file and that works, so i know my program can see the txt file. billz it is just a function not part of a class. And my teacher said no strings and no vectors. only char arrays. – chillpenguin Apr 28 '13 at 05:44
  • 1
    Do you define `saveToFile` _before_ or _after_ you call it? It must be declared (i.e. prototyped) or defined before you call it. – Some programmer dude Apr 28 '13 at 05:46
  • Yes i am using visual studio 2012. The cpp file is in my project of course... – chillpenguin Apr 28 '13 at 05:46
  • @billz Sure in principle but obviously you're introducing a whole new, tangential project here. No code in the body of `saveToFile` would need to be changed but going to STL there could possibly trigger a rewrite of the entire program. – Matt Phillips Apr 28 '13 at 05:46
  • i have a prototype before my main function, and a definition after main. Just like all my other function (and they work fine) so i don't think it has to do with that. – chillpenguin Apr 28 '13 at 05:48
  • "and a definition after main" Humor us and put the definition ahead of main, and see what happens. – Matt Phillips Apr 28 '13 at 05:49
  • How does the prototype look like? It has to be exactly the same as the definition – Some programmer dude Apr 28 '13 at 05:50
  • I put it before main, same error. – chillpenguin Apr 28 '13 at 05:51
  • prototype: void saveToFile(const char fileName[MAX_CHAR], Task list[], int size); – chillpenguin Apr 28 '13 at 05:51
  • Yup the prototype didn't have the 'const' that the definition had. Thanks guys! – chillpenguin Apr 28 '13 at 05:53

1 Answers1

0

If the problem is still unresolved, you should add the path to lib files in visual studio project

properties > Linkers > Additional Library Directories

I faced similar issue and resolved by doing this

dips
  • 114
  • 6