Archived

This topic is now archived and is closed to further replies.

Help using objects of another class in a different class.

This topic is 5153 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a small text game. I am trying to write just a basic example of an attack method. I have it written as so.
class Necromancer : public CPlayer{				//Derived necromancer class

public:								
	Necromancer(){								//Default constructor stats i want for a necromancer

		setHealth(100);
		setMaxHealth(100);
		setMana(60);
		setMaxMana(60);
		setStrength(20);
		setSpeed(60);
		setDefense(40);
		setName(nameSet());
		setClass("Necromancer");
		initSkills();
	}
	void printInfo(){							//Same as above

		system("cls");
		cout<<"Class: "<<getClass()<<endl;
		printBaseInfo();
		cout<<"	Health: "<<getHealth()<<"/"<<getMaxHealth()<<endl;
		cout<<"	Mana: "<<getMana()<<"/"<<getMaxMana()<<endl;
		cout<<"	Strength: "<<getStrength()<<endl;
		cout<<"	Speed: "<<getSpeed()<<endl;
		cout<<"	Defense: "<<getDefense()<<endl;
		printSkills();
	}
	void initSkills(){							//Same as above cept its for necromancer

		skill[0] = "Fire";
		skill[1] = "Flame";
		skill[2] = "Burn";
		skill[3] = "Big Burn";
		skill[4] = "Blaze";
		skill[5] = "Firestorm";
		skill[6] = "Forest Fire";
		skill[7] = "Flame of Demon";
		skill[8] = "Smoke Rings";
		skill[9] = "Hell Fire";
	}
	void attack(CMonster monster){
		monster.setMonsterHealth(monster.getMonsterHealth() - 10);
		cout<<monster.getHealth();
	}
}player;
class CMonster{
private:
	int health;
	int maxhealth;
	int defense;
	int speed;
	int strength;
public:
	CMonster(){
		health = 100;
		maxhealth = 100;
		defense = 100;
		speed = 100;
		strength = 100;
	}
	int getMonsterHealth(){
		return health;
	}
	void setMonsterHealth(int newhealth){
		health = newhealth;
	}
}big;
int chartype;
void getCharacterType(){									
	cout<<"1) Barbarian\n";
	cout<<"2) Necromancer\n";
	cout<<"What kind of character would you like to be?";
	cin>>chartype;
}

void main(){							
	getCharacterType();
	player.attack(big);
}
if you didn't want to read that the part where i'm havin trouble is:
	void attack(CMonster monster){
		monster.setMonsterHealth(monster.getMonsterHealth() - 10);
		cout<<monster.getHealth();
	}
Plz help i'm stuck
Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
[edited by - ssjcory on November 6, 2003 7:46:13 PM] [edited by - ssjcory on November 6, 2003 7:46:52 PM] [edited by - ssjcory on November 6, 2003 7:49:05 PM]

Share this post


Link to post
Share on other sites
Did you try passing monster as a pointer? Like
void attack(CMonster *monster){
monster->setMonsterHealth(monster->getMonsterHealth() - 10);
cout

then..

player.attack(&big);
}


EDIT - Also put "class CMonster;" above the decleration of Necromancer. So the compiler knows it will soon exist.

Jiia

[edited by - Jiia on November 6, 2003 7:53:07 PM]

Share this post


Link to post
Share on other sites
Same error either way:

C:\Documents and Settings\Cory Fisher\Game.cpp(199) : error C2061: syntax error : identifier ''CMonster''
C:\Documents and Settings\Cory Fisher\Game.cpp(236) : error C2660: ''attack'' : function does not take 1 parameters

Thanks



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Share this post


Link to post
Share on other sites
You answered my question in your edit... but is there a way so that I don''t have to keep moving my classes above or below other classes?



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Share this post


Link to post
Share on other sites
Yes, just exactly that way. You can have a list of all of your objects, like..

class CGuy;
class CTree;
class Blah; etc...

Put them above everything else, or in a global header or such. But your code still won't compile because your attack function is inline, and it's doing too much stuff with CMonster before the class is declared. You need to put functions like that in the source file.

EDIT - I'm not sure how far along you are in learning this stuff, so I need to explain further. That list is not the actual classes being declared, but typed exactly as I have it, with your class names. You still have to declare your classes normally after that list is declared.

[edited by - Jiia on November 6, 2003 8:03:39 PM]

Share this post


Link to post
Share on other sites
What if i made a .h file say like all.h?
Then in that header put
#include playerclass.cpp
#include monsterclass.cpp
and then do #include all.h on the top of every file would that work?
Thanx for all your help.



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|



[edited by - ssjcory on November 6, 2003 8:05:28 PM]

Share this post


Link to post
Share on other sites
Don''t #include .cpp files into a header. It''s supposed to be the other way around (you know, a header goes at the top or "head" of a source file, hence the name). Like Jiia was saying, you need to create a list of your classes in the header, then define (i.e., write out all the classes'' member information) the classes below that, but leave out function definitions. Then, create another source file, usually with the same filename as your header but with a .cpp extension. In this source file, #include the header and define all the functions for your classes. Now, in your main source file, #include the header.

Compile, link, build and you''re ready to go.



"Skepticism.... that great rot of the intellect." - V.H.
Bah, what does HE know?


Albekerky Software

Share this post


Link to post
Share on other sites
You don''t want to include cpp files that way. None of the source code (cpp files) needs to be shared through your program. For example, Necromancer class never needs to know how CMonster''s functions work. It just needs to know they exist, and how to call them. That''s why you #include header files. If one source file needs to access or communicate with another object type, then just include the header containing that other object type''s class.

Usually you don''t need an "all.h" type file. If you had seperate source files for Necromancer and CMonster, just make sure the header with both classes is included in it.

Did I even answer your question?

Share this post


Link to post
Share on other sites