I'm trying to build a simple program that accepts and stores contact information (first name, last name, phone number) but ran in to a linking error that has me confused. I can get the program working when all the code is in the same file, but I want to get used to handling multiple files since that seems like a "best practice" and helps clarify my comprehension of the program pieces.
Error message:
1>contactmanager.obj : error LNK2019: unresolved external symbol
"public: class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > __thiscall Contact::getFirstName(void)"
(?getFirstName@Contact@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator
@D@2@@std@@XZ) referenced in function _main
From what I read, the LNK2019 error can occur when you say a method exists, but didn't implement it. I implemented the setFirstName() and getFirstName() methods in the contact.cpp file, so perhaps I coded it incorrectly or messed up the header some how. I've been troubleshooting this for a few hours but an unsure where the problem leis. Any advice is greatly appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// contactmanager.cpp : Defines the entry point for the console application.
#include <iostream>
#include <string>
usingnamespace std;
#include "contact.h"
int main()
{
cout << "Welcome to Contact Manager (v0.1)\n\n";
Contact newEntry;
newEntry.setFirstName("John");
cout << "Your name is: " << newEntry.getFirstName() << endl;
return 0;
}
I added #include <string> in contact.h but still receive the the same error message when I compile/link. I retyped the entire program in another folder but the same error happens.
Interestingly, the error occurs if I completely remove the contact.cpp file from my computer, so it's like the file doesn't even exist. I don't quite grasp how contact.h knows anything about contact.cpp since I never directly refer to the .cpp file. Perhaps that's the source of my problem?
I triple checked that I selected Windows 32 Console Application when starting the new project in MS Visual C++ 2010 Express, as other people reported that could cause errors. Pasting in the entire error in case it helps:
What I did was to #include "contact.cpp" instead of "contact.h" in contactmanager.cpp file.
Oh, and it WORKS!
The reason for it not working is that, there was no implementation of these functions, visible to the linker!
But as people say, be careful when #including .cpp files!
I think I solved the problem with contact.cpp not being included. Turns out I added the file to the folder the wrong way. /eyeroll
In MS Visual C++ 2010, do not add a new file via File -> New -> File and selecting your project folder. It does not automatically include the new file in your project, even though it's with all the other project files.
The proper way to add a new file is to use the Solutions Explorer window via right click on the Source Files folder -> Add -> Add New Item...
The link error has gone away and been replaced by a plethora of compiling errors, so I think progress has been made. Now to sort these out...