Need help fixing a few errors in my program.

The following program is a game i started creating, currently it will not display anything (as i started creating it yesterday), but i need help fixing some of the errors i have created.

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <fstream>
using namespace std;
/*Armor prevents monsters from being
destroyed and must be repaired to work
up to it's maximum capability.*/
class Armor
{
    int Weight;	
    int Armor_Health;
public:
Armor(); 
~Armor();    
    int GetWeight(); 
	void SetWeight(int x);
	int GetArmor_Health(); 
	void SetArmor_Health(int x);
};
/*Shields defend the monsters from most
damage and regenerate after each battle.*/
class Shield
{	
    int Weight;
    int Defense;
    string Resists;
    int Shield_Energy;
public:
Shield(); 
~Shield();    
    int GetWeight(); 
	void SetWeight(int x);
	int GetDefense(); 
	void SetDefense(int x);
	string GetResists(); 
	void SetResists(string x);
	int GetShield_Energy(); 
	void SetShield_Energy(int x);
};
/*Monsters are what you use to battle and
they can hold weapons, armor, and shields.*/
class Monster
{	
    int Weight;
    int Monster_Health;
    int Monster_Armor;
    int Monster_Shields;
    int Monster_Damage;
public:
Monster(); 
~Monster();   
    int GetWeight(); 
	void SetWeight(int x);
	int GetMonster_Health(); 
	void SetMonster_Health(int x);
	int GetMonster_Armor(); 
	void SetMonster_Armor(int x);
	int GetMonster_Shields(); 
	void SetMonster_Shields(int x);
	int GetMonster_Damage(); 
	void SetMonster_Damage(int x);
	void Level1();
};
void Monster::Level1() //Default Name
{	
    Weight = 50;
    Monster_Health = 50;
    Monster_Armor = 0;//Not integrated yet.
    Monster_Shields = 0;//Not integrated yet.
    Monster_Damage = 0;//Not integrated yet.
}
void MonsterAttack(Monster & Monster1, Monster & Monster2)
{
    //Tests if the monster's shield is equal to 0, if so damage is subtracted from health.
    if(Monster2.GetMonster_Shields() == 0)
    {Monster2.SetMonster_Health(Monster2.GetMonster_Health() - Monster1.GetMonster_Damage());}
    //Tests if the monster's shield is greater than 0, if so damage is subtracted from the shield and monster's health.
    if(Monster2.GetMonster_Shields() > 0)
    {Monster2.SetMonster_Shields(Monster2.GetMonster_Shields() - Monster1.GetMonster_Damage());
    Monster2.SetMonster_Health(Monster2.GetMonster_Health() - Monster1.GetMonster_Damage() / 10);}
    //Tests if the monster's shield is less than 0, if so the shield's value is equal to 0.
    if(Monster2.GetMonster_Shields() < 0)
    {Monster2.SetMonster_Shields(0);}
    //Tests if the monster's health is less than 0, if so the monsters's health is equal to 0.
    if(Monster2.GetMonster_Health() < 0)
    {Monster2.SetMonster_Health(0);}
    //Tests if the monster's health is equal to 0, if so the monsters's shield is equal to 0.
    if(Monster2.GetMonster_Health() == 0)
    {Monster2.SetMonster_Shields(0);}
}
void BattlePhase (Monster & Monster1, Monster & Monster2)//does the battle phase
{
    while (Monster1.GetMonster_Health() > 0 && Monster2.GetMonster_Health() > 0)
    {
    MonsterAttack(Monster1, Monster2);
    MonsterAttack(Monster2, Monster1);
    }
}
int main(int argc, char *argv[])
{	
    Monster YourMonster1;
	Monster OpponentMonster1;
    YourMonster1.Level1();
    OpponentMonster1.Level1();
    BattlePhase (YourMonster1, OpponentMonster1);
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return 0;
}
Last edited on
You need to put a ; at the end of each class declaration section.

Example:
1
2
3
4
5
class Foo
{
     public:
        Foo();
};  //<---------you forgot this 


I think this is supposed to be a constructor void Monster::Class1() but it should look like this Monster::Monster()

if(Monster2.GetMonster_Shields() = 0) look carefully at the = in this statement.

{Monster2.SetMonster_Health(Monster2.GetMonster_Health() - Monster1.GetMonster_Damage())} Missing ; there are more of these but i'm not pointing them all out.
Last edited on
Edited above post. As of now, I'm stuck with 2 more errors i cannot fix.

67 C:\Dev-Cpp\newgame.cpp no `void Monster::Level1()' member function declared in class `Monster' 
//above is supposed to be a monster
C:\Dev-Cpp\newgame.cpp In function `void MonsterAttack(Monster&, Monster&)': 
82 C:\Dev-Cpp\newgame.cpp `1.00000000000000005551115123125782702118158340454e-1' cannot be used as a function  
Last edited on
Line 67, you're attempting to implement Monster::Level1 (), but there is no such function in the class declaration.

Line 82, you've got an extraneous set of parenthises after the 0.10.
Monster1.GetMonster_Damage * 0.10());


ok i fixed it but now I am getting a ton of linker errors, some repeats...
  [Linker error] undefined reference to `Monster::GetMonster_Health()' 
  [Linker error] undefined reference to `Monster::GetMonster_Shields()' 
  [Linker error] undefined reference to `Monster::GetMonster_Damage()' 
and many more.
Last edited on
Not sure if what you posted is the extent of your code.
If it is, the linker errors are from not implementing the named functions.
Of the three you listed, I see them declared, but don't see them implemented.
You can't call something that's not implemented.
You have declared a bunch of functions in the class, but not defined them anywhere. This is true for all of your classes. You need to define them like this, for example, and outside the class:

1
2
3
int Monster::GetMonster_Health() {
    //your code here
}


Normally one has the class declarations in their own header file (Monster.h say) and the function definitions in an implementation file (Monster.cpp say).

There are a couple of naming conventions that are reasonably standard but personal preference. Put a leading m_ on all al the class member variables - this helps you to realise it's a member variable, which can be handy sometimes. I put a leasing 'C' on my class names - such as CMonster say.

Also try to avoid having a get & set function for every class variable. We have just had a huge discussion about this here:

http://www.cplusplus.com/forum/lounge/101305/


At your level of programming, I don't have a problem with get functions, but the biggest problem is the set functions - you wouldn't allow anyone to set your real world bank balance to 0.00 would you ?

Instead think about why you need to change the value. You could for example have an ApplyDamage class function instead, as a very simple alternative. At least this way you are not blindly setting the value of m_Health, you are applying a damage value to it:

m_Health -= TheDamage;

And you have the opportunity to doing checking.

Even better, have a SendDamage function in each object that is doing damage to something, and have a ReceiveDamage function in each object that is having damage done to it. The SendDamage function calls the other object's ReceiveDamage function, with the amount of damage as an argument. The ReceiveDamage function uses that information to apply damage to the m_Health variable. No need for the ApplyDamage function in this scenario, class member functions have direct access to class member variables.

That way, you are hiding the internal details of your class from the outside world - which is a good thing.


Also try to learn about constructors & initialiser lists.

With your function names, I personally would not have 'Monster' in all the Monster function names, because you already have it in the object variable name, and it makes everything too long.

Hope all goes well :)
Registered users can post here. Sign in or register to post.