Troubleshooting error LNK2019: unresolved external symbol

Hello folks,

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>
using namespace 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;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// contact.h: Header file for Contact object. 

#ifndef CONTACT_H
#define CONTACT_H

class Contact
{
private:
	string firstName;

public:
	Contact() // Constructor initalizes variables to prevent junk values
	{
		firstName = "";
	}

	void setFirstName(string s);
	string getFirstName();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
// contact.cpp: Contains implementation of Contact object

#include "contact.h"

void Contact::setFirstName(string s)
{
	firstName = s;
}

string Contact::getFirstName()
{
	return firstName;
}


If it matters any, I'm using Microsoft Visual C++ 2010.

-- David G. / EtDecius
hey pal,
it's working fine on my PC..i checked it out..
please add #include <string> in contact.h
Thanks for the reply.

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:


1>------ Build started: Project: contactmanager, Configuration: Debug Win32 ------
1>contactmanager.obj : error LNK2019: unresolved external symbol "public: void 
__thiscall Contact::setFirstName(class std::basic_string<char,struct 
std::char_traits<char>,class std::allocator<char> >)" 
(?setFirstName@Contact@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocat
or@D@2@@std@@@Z) referenced in function _main
1>E:\Programs\Practice\contactmanager\Debug\contactmanager.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
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!
Thanks all for the response.

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...

Thanks again and have a great weekend!

- David G. / EtDecius
Last edited on
Topic archived. No new replies allowed.