Need help fixing a few errors in my program.

Pages: 12
The following program is a game i started creating. 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#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(){return Weight;}
	void SetWeight(int x){Weight = x;}
	int GetArmor_Health(){return Armor_Health;} 
	void SetArmor_Health(int x){Armor_Health = x;}
	void Steel1();
};
/*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(){return Weight;} 
	void SetWeight(int x){Weight = x;}
	int GetDefense(){return Defense;} 
	void SetDefense(int x){Defense = x;}
	string GetResists(){return Resists;} 
	void SetResists(string x){Resists = x;}
	int GetShield_Energy(){return Shield_Energy;} 
	void SetShield_Energy(int x){Shield_Energy = x;}
	void Spectral1();
};
/*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;
    Armor slot1;
    Shield slot2;
public:
Monster(){}; 
~Monster(){};   
    int GetWeight(){return Weight;}  
	void SetWeight(int x){Weight = x;}
	int GetMonster_Health(){return Monster_Health;}  
	void SetMonster_Health(int x){Monster_Health = x;}
	int GetMonster_Armor(){return Monster_Armor;} 
	void SetMonster_Armor(int x){Monster_Armor = x;}
	int GetMonster_Shields(){return Monster_Shields;}  
	void SetMonster_Shields(int x){Monster_Shields = x;}
	int GetMonster_Damage(){return Monster_Damage;}  
	void SetMonster_Damage(int x){Monster_Damage = x;}
	void Level1();
};
void Armor::Steel1() 
{	
    Weight = 50;
    Armor_Health = 150;
}
void Shield::Spectral1()
{	
    Weight = 50;
    Defense = 90;
    Resists = "none";
    Shield_Energy = 100;
}
void Monster::Level1() //Default Name
{	
    slot1 = Steel1();
    slot2 = Spectral1();
    Weight = 50;
    Monster_Health = 50;
    Monster_Armor = 0;//Not integrated yet. Should be steel 1 stats.
    Monster_Shields = 0;//Not integrated yet. Should be spectral 1 stats
    Monster_Damage = 10;

}
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);
    //Temporary display method for testing purposes.
    cout << "Monster1 Health " << Monster1.GetMonster_Health() << endl;
    cout << "Monster1 Shields " << Monster1.GetMonster_Shields() << endl;
    cout << "Monster2 Health " << Monster2.GetMonster_Health() << endl;
    cout << "Monster2 Shields " << Monster2.GetMonster_Shields() << endl;
    }
}
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 :)
Ok I fixed most of the linker errors, but I have a few more that i need help getting rid of...
[Linker error] undefined reference to `Monster::Monster()' 
  [Linker error] undefined reference to `Monster::Monster()' 
  [Linker error] undefined reference to `Monster::~Monster()' 
  [Linker error] undefined reference to `Monster::~Monster()' 
  [Linker error] undefined reference to `Monster::~Monster()' 
  [Linker error] undefined reference to `Monster::~Monster()' 
in addition to fixing the linker errors i also am having trouble implementing the armor and shields
in lines 89 and 90 i want Monster_Armor = shield1 and Monster_Shields = spectral1
i tried doing that by doing lines 54,55,85,86 but it isn't working

see above edited code
Last edited on
Same story - you haven't written a definition for these functions. Most of them are destructors (they have the ~)- you probably don't need them - the compiler will make some implicitly.

How are you going with the other ideas I mentioned?
everything else went good, but i swear i declared this

line 57,58 see, so how do i fix this
You may have declared them, but you haven't defined them. Nowhere in your code have you defined what the constructor and destructor actually do.

brandonator wrote:
everything else went good,


There are still a number of things I mentioned which you haven't done yet :)

With the non defined functions, you have to realise that the compiler needs to know what (definition or implementation) to do. The declaration just tells the compiler that the function definition exists somewhere.
Thanks MikeyBoy
Nowhere in your code have you defined what the constructor and destructor actually do.
i forgot to add the {} after the constructor and destructor, thats what i get for not doing c++ for over half a year :P.
Last edited on
Well, you might want to use those constructors for the purpose they're actually intended - initialising your objects to a known, sensible initial state.
I got the program to work, thanks to your guys advice, but now when i try and add line 85 and 86, because I'm later going to use them as values in my monster for example health and shields, it comes up with two errors
C:\Dev-Cpp\newgame.cpp In member function `void Monster::Level1()': 
85 C:\Dev-Cpp\newgame.cpp `Steel1' undeclared (first use this function) 
(Each undeclared identifier is reported only once for each function it appears in.) 
86 C:\Dev-Cpp\newgame.cpp `Spectral1' undeclared (first use this function) 
If you could show me how to fix that problem that would be great.
Last edited on
Those are methods on other classes, so you need to call them on objects of those classes, just like you do for Monster methods in your MonsterAttack function.

can you show me an example please?
Last edited on
brandonator wrote:
can you show me an example please?
MikeyBoy wrote:
just like you do for Monster methods in your MonsterAttack function
L B, i keep trying and every time i try to do that it comes up with more errors, I'm not sure how to do it, so i need help.
1
2
3
4
5
void Monster::Level1() //Default Name
{	
    slot1 = Steel1();
    slot2 = Spectral1();
//etc 


You can't do that.

slot1 and slot2 are armor and shield objects respectively, Steel1() and Spectral1() are functions with return type void.
However, by looking at your functions, I can see what you were trying to achieve.

Will edit this and give valuable feedback when I'm not so tired.
Pages: 12