having a problem with my code

I am having trouble altering a class in my program
it will act like it is setting a value to the class
but when I access the value it doesn't seem to have
changed. Any feedback would be nice here is the code
Compmon.h
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
#include <iostream>
#include <tuple>

using namespace std;
using namespace tr1;
typedef tuple <int, int, int, int, int, int> Values;
class Compmon{
public:
	Compmon(){};
	~Compmon(){};
	void operator = (Compmon Compmon1);
	void SetValues(int attack, int defense, int health, int attackUp, int defenseUp, int healthUp );
	void SetValues(Values values);
	int GetAttack() const {return itsAttack;};
	int GetDefense() const {return itsDefense;};
	int GetHealth() const {return itsHealth;};
	int GetLevel() const {return itsLevel;};
	Values GetValues() ;
	void LevelUp(int levelsUp);
	int itsAttack;
	int itsDefense;
	int itsHealth;
	int itsLevel;
	int itsAttackUp;
	int itsDefenseUp;
	int itsHealthUp;
};
void Compmon::SetValues(int attack, int defense, int health, int attackUp, int defenseUp, int healthUp) {
	itsAttack=attack;
	itsDefense=defense;
	itsHealth=health;
	itsLevel=1;
	itsAttackUp=attackUp;
	itsDefenseUp=defenseUp;
	itsHealthUp=healthUp;
}
void Compmon::LevelUp(int levelsUp){
	itsLevel=itsLevel+levelsUp;
	cout << "your compmon is now level " << itsLevel;
	cout << "\n\n";
	itsAttack=itsAttack+itsAttackUp*levelsUp;
	itsDefense=itsDefense+itsDefenseUp*levelsUp;
	itsHealth=itsHealth+itsHealthUp*levelsUp;
}
Values Compmon::GetValues() {
	Values values(itsAttack, itsDefense, itsHealth, itsAttackUp, itsDefenseUp, itsHealthUp);
	return values;
}
void Compmon::operator =(Compmon Compmon1){
	SetValues(Compmon1.GetValues());
}
void Compmon::SetValues(Values values){
	itsAttack=get<0>(values);
	itsDefense=get<1>(values);
	itsHealth=get<2>(values);
	itsLevel=1;
	itsAttackUp=get<3>(values);
	itsDefenseUp=get<4>(values);
	itsHealthUp=get<5>(values);
}

Trainer.h
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
#include "compmon class.h"

class Trainer{
public:
	Trainer(Compmon Compmon1, Compmon Compmon2, Compmon Compmon3, Compmon Compmon4, Compmon Compmon5, Compmon Compmon6);
	~Trainer(){};
	Compmon GetCompmon(int compmonNumber);
	void ChangeCompmon(int compmonNumber, Compmon NewCompmon);
private:
	Compmon itsCompmon1;
	Compmon itsCompmon2;
	Compmon itsCompmon3;
	Compmon itsCompmon4;
	Compmon itsCompmon5;
	Compmon itsCompmon6;
};
Trainer::Trainer(Compmon Compmon1, Compmon Compmon2, Compmon Compmon3, Compmon Compmon4, Compmon Compmon5, Compmon Compmon6){
	itsCompmon1=Compmon1;
	itsCompmon2=Compmon2;
	itsCompmon3=Compmon3;
	itsCompmon4=Compmon4;
	itsCompmon5=Compmon5;
	itsCompmon6=Compmon6;
}
Compmon Trainer::GetCompmon(int compmonNumber) {
	switch(compmonNumber){
		case (1):
			return itsCompmon1;
		case (2):
			return itsCompmon2;
		case (3):
			return itsCompmon3;
		case (4):
			return itsCompmon4;
		case(5):
			return itsCompmon5;
		case (6):
			return itsCompmon6;
		default :
			cout << "error 1";
	}
}
void Trainer::ChangeCompmon(int compmonNumber, Compmon NewCompmon){
	switch(compmonNumber){
		case (1):
			itsCompmon1=NewCompmon;
		case (2):
			itsCompmon2=NewCompmon;
		case (3):
			itsCompmon3=NewCompmon;
		case (4):
			itsCompmon4=NewCompmon;
		case (5):
			itsCompmon5=NewCompmon;
		case (6):
			itsCompmon6=NewCompmon;
		default :
			cout << "error 2";
	}
}

compmon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "trainer class.h"

int main(){
	Compmon pixey;
	Compmon null;
	int question;
	null.SetValues(0,0,0,0,0,0);
	pixey.SetValues(2,2,13,2,2,3);
	Trainer You(pixey,null,null,null,null,null);
	You.GetCompmon(1).LevelUp(4);
	cout << "welcome to the world of compmon !\n";
	cout << "you're first compmon is a pixey.\n";
	cout << "Pixey\nLevel "<<You.GetCompmon(1).GetLevel()<<"\nAttack "<<You.GetCompmon(1).GetAttack()<<"\nDefense "<<You.GetCompmon(1).GetDefense()<<"\nHealth "<<You.GetCompmon(1).GetHealth()<<"\n";
	cin  >> question;
	return 0;
}
1
2
Compmon Trainer::GetCompmon(int compmonNumber) //returns a copy
You.GetCompmon(1).LevelUp(4); //modifies a copy 


Also, take a look at arrays.
Thanks, do you have any ideas on how
to fix this? How to make it so the function
getCompmon doesn't return a copy?
How to make it so the function getCompmon doesn't return a copy?
You can return a pointer to your objects instead without major changes in the way your program works (meaning you just have to use -> instead of . etc).

Another approach would be to use reference but maybe you would be forced to change the program a bit.
Last edited on
Sorry eypros... even i am not able to understand what you are saying . can you elobrate on this .. please .
Thank you eypros I figured out my problem. I edited the Trainer.h file to include pointers then
changed the way I accessed the variables, also I am using arrays to allow for less coding in the
classes. Here is the new code that I created.
Trainer.h
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
#include "compmon class.h"

class Trainer{
public:
	Trainer();
	~Trainer(){};
	Compmon * GetCompmon(int compmonNumber);
	void ChangeCompmon(int compmonNumber, Compmon NewCompmon);
private:
	Compmon * pItsCompmon[6];
	Compmon itsCompmon[6];
};
Trainer::Trainer(){
	itsCompmon[1]=Null;
	itsCompmon[2]=Null;
	itsCompmon[3]=Null;
	itsCompmon[4]=Null;
	itsCompmon[5]=Null;
	itsCompmon[0]=Null;
	pItsCompmon[0]=&itsCompmon[0];
	pItsCompmon[1]=&itsCompmon[1];
	pItsCompmon[2]=&itsCompmon[2];
	pItsCompmon[3]=&itsCompmon[3];
	pItsCompmon[4]=&itsCompmon[4];
	pItsCompmon[5]=&itsCompmon[5];
}
Compmon * Trainer::GetCompmon(int compmonNumber) {
		return pItsCompmon[compmonNumber-1];
}
void Trainer::ChangeCompmon(int compmonNumber, Compmon NewCompmon){
		itsCompmon[compmonNumber-1]=NewCompmon;
}

Compmon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "trainer class.h"

int main(){
	int question;
	Null.SetValues(0,0,0,0,0,0);
	Trainer You;
	You.GetCompmon(1)->SetValues(2,2,13,2,2,3);
	You.GetCompmon(1)->LevelUp(4);
	cout << "welcome to the world of compmon !\n";
	cout << "you're first compmon is a pixey.\n";
	cout << "Pixey\nLevel "<<You.GetCompmon(1)->GetLevel()<<"\nAttack "<<You.GetCompmon(1)->GetAttack()<<"\nDefense "<<You.GetCompmon(1)->GetDefense()<<"\nHealth "<<You.GetCompmon(1)->GetHealth()<<"\n";
	cin  >> question;
	return 0;
}
Topic archived. No new replies allowed.