The Classical "Unresolved External Symbol"

Hello there!
I am currently having some C++ lessons and some of us keep getting the "unresolved external symbol" error. Our teacher never cannot even guess why this is so that is why I turn to you.

Latest example:

The program is going to count votes (yes, no and blank) and in the header file I have the public method "add".

class Elect
{
private:
int yes, no, blank;
public:
void add (int addyes, int addno, int addblank);

[...]
}

I've located the error to the line

void add (int addyes, int addno, int addblank);

If I make it

void add (int addyes, int addno);

it works. So the C++ basically doesn't let me have three (or more) parameters, just two.
Why is that? ._.
And why is that an "unresolved external symbol" error? >_>

Thanks in advance!
Unresolved external error means the compiler found the prototype for a function, but you never gave that function a body (or the linker can't find the body).

This often happens when the parameter list of the prototype does not exactly match the parameter list of the function definition (resulting in them being two separate functions).

To further diagnose, please post how your add() function body is defined. It should look exactly like this:

1
2
3
4
void Elect::add(int addyes,int addno,int addblank)
{
  // code here
}
Currently it looks like this

1
2
3
4
5
6
void Elect :: add (int addyes, int addno, int addblank)
{
	yes=yes+addyes;
	no=no+addno;
	blank=blank+addblank;
}


but even getting rid of all the variables does not solve the problem;

1
2
3
4
void Elect :: add (int addyes, int addno, int addblank)
{
	cout<<"Uncoded segment";
}


To be more specific the error message is

1
2
elect_main.obj : error LNK2001: unresolved external symbol "public: void __thiscall Elect::add(int,int,int)" (?add@Elect@@QAEXHHH@Z)
elect_main.obj : error LNK2001: unresolved external symbol "public: __thiscall Elect::Elect(void)" (??0Elect@@QAE@XZ)
I take it those functions are in a separate cpp file from elect_main.cpp

The problem is that cpp file is not part of your project. As a result, it's not being linked to, which is why the linker is not finding those function bodies.

Assuming this is VS, go in the menu Project | Add Existing Item. Then select whatever cpp file that is. Then rebuild.

EDIT:

Also in the future when you create a new cpp file, do it by going to Project | Add New Item, rather than File | New which is probably what you did last time.
Last edited on
Sorry for the late response, but it seems what you said was the problem in many of our projects.
Thank you very much! : D
Our teacher never cannot even guess why this is so that is why I turn to you.

I wonder how all these people get jobs where they supposedly "teach" something they don't really know and couldn't even be bothered to google for.
the professor was unable to resolve your question because you forgot to link them to google... please add google to your professor's project list and maybe it can be resolved through them next time...
Topic archived. No new replies allowed.