6 Replies - 506 Views - Last Post: 23 April 2009 - 06:21 PM Rate Topic: -----

#1 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Object undefined?

Post icon  Posted 23 April 2009 - 05:30 PM

I'm making a fairly simple text-based fighting game just for the heck of it and I'm getting these errors:
F:\Documents\Code\game.cpp: In member function `void Person::Fight()':
F:\Documents\Code\game.cpp:83: error: request for member `type' in `opp', which is of non-class type `Monster ()(float, std::string)'

F:\Documents\Code\game.cpp:84: error: request for member `HP' in `opp', which is of non-class type `Monster ()(float, std::string)'
F:\Documents\Code\game.cpp:85: error: request for member `Dmg' in `opp', which is of non-class type `Monster ()(float, std::string)'
F:\Documents\Code\game.cpp:86: error: request for member `Armor' in `opp', which is of non-class type `Monster ()(float, std::string)'

F:\Documents\Code\game.cpp: In constructor `Monster::Monster(float, std::string)':
F:\Documents\Code\game.cpp:106: error: `you' undeclared (first use this function)
F:\Documents\Code\game.cpp:106: error: (Each undeclared identifier is reported only once for each function it appears in.)


for this code:
#include <iostream>
#include <cstdlib>
using namespace std;
class Game {
public:
	void PlayGame();
};
class Combatant {
public:
	int HP;
	int Dmg;
	int Armor;
};
class Person : public Combatant {
public:
	int MaxHP;
	Person();
	void Fight();
	void NewEvent();
	bool HasLost();
};
class Monster : public Combatant {
public:
	string type;
	Monster(float factor, string type);
	bool IsDead();
};
int main(int argc, char* argv[]) {
	srand(time(NULL));
	Game main;
	int choice;
	cout << "1. Play Game\n2. Exit\n\nChoice: ";
	cin >> choice;
	switch(choice) {
		case 1:
			main.PlayGame();
		break;
		case 2:
			return 0;
		break;
		default:
			cout << "Incorrect input!\n";
		break;
	}
}
void Game::PlayGame() {
	Person you;
	while (!you.HasLost()) {
		you.NewEvent();
	}
}
Person::Person() {
	HP = 100;
	MaxHP = 100;
	Dmg = 5;
	Armor = 5;
}
void Person::Fight() {
	int MonsterType = rand() % 10 + 1;
	string type;
	float factor;
	if (MonsterType == 1 || MonsterType == 2 || MonsterType == 3 || MonsterType == 4) {
		factor = rand() % 25 + 50;
		factor /= 100;
		type = "mercenary";
	}
	else if (MonsterType == 5 || MonsterType == 6 || MonsterType == 7) {
		factor = rand() % 25 + 75;
		factor /= 100;
		type = "soldier";
	}
	else if (MonsterType == 8 || MonsterType == 9) {
		factor = rand() % 50 + 100;
		factor /= 100;
		type = "mage";
	}
	else {
		factor = rand() % 50 + 150;
		factor /= 100;
		type = "dragon";
	}
	Monster opp(float factor, string type);
	cout << "You have encountered a " << opp.type << "!\n";
	cout << "HP: " << opp.HP << "\n";
	cout << "Damage: " << opp.Dmg << "\n";
	cout << "Armor: " << opp.Armor << "\n";
}
void Person::NewEvent() {
	int EventType = rand() % 6 + 1;
	if (EventType == 1 || EventType == 2 || EventType == 3)
		Fight();
	else if (EventType == 4)
		Dmg++;
	else if (EventType == 5)
		MaxHP += rand() % 5 + 5;
	else
		Armor++;
}
bool Person::HasLost() {
	if (HP <= 0)
		return true;
	else
		return false;
}
Monster::Monster(float factor, string type) {
	HP = you->HP * factor;
	Dmg = you->Dmg * factor;
	Armor = you->Armor * factor;
	this->type = type;
}
bool Monster::IsDead() {
	if (HP <= 0)
		return true;
	else
		return false;
}


I've tried the . and -> operators and both failed. Any suggestions would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Object undefined?

#2 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Object undefined?

Posted 23 April 2009 - 05:52 PM

Whenever you declare an object, don't put the types in the (), ex:

Monster opp(float factor, string type); on line 83 should be Monster opp(factor, type);
Was This Post Helpful? 1
  • +
  • -

#3 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Re: Object undefined?

Posted 23 April 2009 - 06:00 PM

Wow...I don't know what the heck I was thinking...anyways, I still have this error:
F:\Documents\Code\game.cpp: In constructor `Monster::Monster(float, std::string)':
F:\Documents\Code\game.cpp:106: error: `you' undeclared (first use this function)
F:\Documents\Code\game.cpp:106: error: (Each undeclared identifier is reported only once for each function it appears in.)


Thanks for that though
Was This Post Helpful? 0
  • +
  • -

#4 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Object undefined?

Posted 23 April 2009 - 06:05 PM

Well, that's because you never declared "you" in any scope available to that class or that constructor, just like the error says. If you want it to have access to that information, I suggest you make it so that you can pass it to the constructor.
Was This Post Helpful? 0
  • +
  • -

#5 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Re: Object undefined?

Posted 23 April 2009 - 06:10 PM

How would I go about doing that? Sorry, but I haven't been using c++ lately, forgotten a lot of it...
Was This Post Helpful? 0
  • +
  • -

#6 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Object undefined?

Posted 23 April 2009 - 06:16 PM

Well, you'd add the parameters to the constructor so it'd be like this:

Monster::Monster(float factor, string type, int yourHP, int yourDmg, int yourArmor) {
	HP = yourHP * factor;
	Dmg = yourDmg * factor;
	Armor = yourArmor * factor;
	this->type = type;
}



Then in the Fight function you'd call it as:

	Monster opp(factor, type,HP,Dmg,Armor);


Was This Post Helpful? 0
  • +
  • -

#7 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Re: Object undefined?

Posted 23 April 2009 - 06:21 PM

Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1