• 12
• 12
• 9
• 10
• 13

# LNK 2019 and LNK 2001 linking errors using inheritence

This topic is 2509 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I hate programming, for some reason I can't find the error in the code below.
Note that when I don't inherit Problem1 from IProblem everything works fine.

[source]//main.cpp

#include <iostream>
#include "Problem1.h"

int main()
{
Problem1 myproblem;
myproblem.Solve();

int dontexit;
std::cin >> dontexit;

return 0;
}

//IProblem.h

class IProblem
{

public:
virtual void Solve() =0;
virtual int GetID() const =0;

virtual ~IProblem() {};
};

//Problem1.h

#include "IProblem.h"

class Problem1 : public IProblem
{
public:

void Solve();
int GetID() const;

private:
int m_ID;
};

//Problem1.cpp

#include <iostream>
#include "Problem1.h"

int Problem1::GetID() const {return m_ID;}

void Problem1::Solve()
{

std::cout << "blabla" << std::endl;
}

[/source]

##### Share on other sites
You have an extra semicolon after your destructor declaration. I'm not sure if that's enough to cause an error (especially a linker error), but that's the only problem I can see. Can you post the exact errors you're getting?

##### Share on other sites
Need a bit more information. What API/IDE are you using? It appears to be a console app. Correct? What are the exact errors you're getting? Need the full text.

The code looks okay, so it may be that you're not linking the proper libraries or don't have the correct default directories, etc. What are your project settings, in particular, the paths to your libraries and include files.

EDIT: Your code, as-is, doesn't have the problem, but you need to set m_id before you use it.

##### Share on other sites
I just choose empty project from the wizard.
I'm getting the error below:

 1> main.cpp 1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Problem1::Solve(void)" (?Solve@Problem1@@UAEXXZ) referenced in function _main 1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall Problem1::GetID(void)const " (?GetID@Problem1@@UBEHXZ) 1>C:\Users\robvleugel\Documents\Visual Studio 2010\Projects\Test\Debug\Test.exe : fatal error LNK1120: 2 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

EDIT:

Hm I just started a new empty project and used these source files, and everything compiles fine. Although I still want to know what caused the linking problems I think I'm not going to investigate this any further. My guess is that the project files somehow got corrupt after I changed some folder names as I was not satishfied with the name I gave it.

Thanks buckeye for your advice on not initializing m_ID, I totally overlooked that.