Linker Error - unresolved external symbol

So I'm a Java programmer that's learning C++, and I decided to write a basic RPG-type game to teach myself about headers, pointers, libraries, etc. Everything compiled and linked fine, until I started referencing other source files in my main() method. Briefly; I have a 'Character' object that I try to construct in my main method, and I get this error:

error LNK2019: unresolved external symbol "public: __thiscall Character::Character(void)" (??0Character@@QAE@XZ) referenced in function _main


The research I've done suggests that it can result from failing to tell your linker to look for outside libraries/headers, or if you have prototype functions without definitions. All of these sources are internal to my project, and all of the prototypes in my headers have definitions, as far as I can tell. I can include the body of Character.h and Character.ccp files if needed, but they're rather lengthy with a lot of filler, but I'm assuming my error is with formatting outside of the body.

Character.cpp:

1
2
3
4
5
6
7
#include "Equipment.h"

class Character{

//body

};


Character.h:

1
2
3
4
5
6
7
8
9
10
11
12
#ifndef _CHARACTER_
#define _CHARACTER_

#include "Equipment.h"

class Character{

//body

};

#endif 


Main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Character.h"
#include "Equipment.h"
#include "Constants.h"
#include "Fileio.h"
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char** argv){

	Character ch = Character();

	return 0;
}
Last edited on
1. By normal conventions, Character.h should be called by Character.cpp.
2. If step 1 above is done, then you don't need to re-define the class in Character.cpp. Just define all of the inner-working functions (but that can be done in the header too if it's a small class making the cpp file unnecessary.
3. Now for your actual problem: You don't explicitly state a constructor that takes no arguments. That's why it can't find anything to excecute when you do Character();

Unlike in Java, where you need to use Character(), in C++ it will automatically call a default constructor when you do: Character ch;. You only need that assignment if ch is a pointer like so:
Character* ch = Character();

If ch is not a pointer, you could use non-default constructors like so:
Character ch(a,b,c);

That would call a constructor with 3 arguments matching the types of a, b and c.
I do have a constructor with no arguments for Character, I just left out the body of the class because it's rather lengthy and contains almost exclusively basic fields, mutators, and fetch methods. I get the same error even if I don't explicitly call a constructor, or if I call a constructor that does have parameters.

To clarify the other points: When you say I don't need to re-define the class in Character.cpp, do you mean the method definitions don't need class Character { . . . } around them? And header files can have non-prototype methods in them?

Another question, does one need to #include the header file in the file that defines its methods (ex. #include Character.h in Character.cpp)? I'm unclear on how the linker/compiler knows where to look for header definitions
Last edited on
Topic archived. No new replies allowed.