Hi guys new here need help on inherintance

Ok so I am in the procces of making a little yugioh game. For those who have never heard of it its a fairly popular or atleast it used to be back in the old days. I am having a little trouble on inherintance constructor thought yu guys could lend me a hand=). Heres the code I will comment the line that I get an error on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
using namespace std;


class MonsterCard{

protected:
	string Name;
	int AttackPoints;
	int DefencePoints;
	int StarLevel;


public:
	MonsterCard();
	MonsterCard(string names,int attkpoints,int defpoints, int strlvl);
	string getName();
	void setName(string names);
	int getAttackPoints();
	void setAttackPoints(int attkpoints);
	int getDefencePoints();
	void setDefencePoints(int defpoints);
	int getStarLevel();
	void setStarLevel(int starlvl);
	void attack(MonsterCard enemy);

};

class DarkMagician : public MonsterCard{
public:
	DarkMagician();

};


MonsterCard::MonsterCard(string names,int attkpoints,int defpoints, int strlvl){
	Name = names;
	AttackPoints = attkpoints;
	DefencePoints = defpoints;
	StarLevel = strlvl;
}
string MonsterCard::getName(){
	return Name;
}
int MonsterCard::getAttackPoints(){
	return AttackPoints;
}
int MonsterCard::getDefencePoints(){
	return DefencePoints;
}
int MonsterCard::getStarLevel(){
	return StarLevel;
}
void MonsterCard::setName(string names){
	Name = names;
}
void MonsterCard::setAttackPoints(int attkpoints){
	AttackPoints = attkpoints;
}
void MonsterCard::setDefencePoints(int defpoints){
	DefencePoints = defpoints;
}
void MonsterCard::setStarLevel(int strlvl){
	StarLevel = strlvl;
}
void MonsterCard::attack(MonsterCard enemy){
	int EnemyAttackPoints = enemy.getAttackPoints();
	string EnemyName = enemy.getName();

	cout << Name << " Has Attacked " << EnemyName << endl;

	if(AttackPoints > EnemyAttackPoints){
		cout << Name << " Has Destroyed Opponents " << EnemyName << endl;
	}else if(AttackPoints < EnemyAttackPoints){
		cout << EnemyName << " Has Destroyed Opponents" << Name << endl;
	}else if(AttackPoints == EnemyAttackPoints){
		cout << Name << " And Opponents " << EnemyName << " Have Been Destroyed"; 
	}


}
DarkMagician::DarkMagician(){
	MonsterCard("DarkMagician",2500,2000,7);
}

int main(){

	 DarkMagician dm = DarkMagician();
	 
	
	
	


	system("pause");
	return 0;
}


Here is the error I am getting on compiler.
main.obj : error LNK2019: unresolved external symbol "public: __thiscall MonsterCard::MonsterCard(void)" (??0MonsterCard@@QAE@XZ) referenced in function "public: __thiscall DarkMagician::DarkMagician(void)" (??0DarkMagician@@QAE@XZ)
You've got a number of problems here; let's deal one at once.

1. Your error is saying that the linker can't find where you've defined the body of the MonsterCard::MonsterCard(void) function (the default constructor).

If you look through your code, you'll find it's right - you've declared it at line 16, but never defined it.

So, one solution would be to define it. Another would be to remove the declaration; then the compiler will provide you with a default version - for free!

However, the question remains - why is the default constructor being used at all? You're calling the constructor that takes string and int parameters.

2. Your DarkMagician::DarkMagician() constructor is slightly malformed (line 83).

What you should have done was to use what's called an initialiser list and called the base-class constructor in that.

1
2
3
4
DarkMagician::DarkMagician() : MonsterCard("DarkMagician", 2500, 2000, 7)
{
    // Any other initialisation can go here
}


As you wrote it, because you've not provided an implicitexplicit base-class call, the compiler is automatically calling the default base class constructor for you, which is MonsterCard(void), and as we know - there's no body defined for that function.

Also, by writing what you did for that constructor, what you've effectively done is to construct the DarkMagician as a default MonsterCard (line 83) and then line 84 creates a local-scope MonsterCard instance which is then destructed again when the function returns.

Hope that helps!

Cheers
Jim
Last edited on
Jim you are freaking awesome!
Thanks
Topic archived. No new replies allowed.