Help about my error

Sory for my bad english, but

i'm new in c++ and i read few book and make samo code!

So!

My class is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Monster
{
public:
	Monster(int hp, int acc, int xpReward, int armor, const char* weaponName, int lowDamage, int highDamage);
	bool isDead();
	COORD pos;
	int getXPReward();
	int getArmor();

	void attack(CPlayer& player);
	void takeDamage(int damage);
	void displayHitPoints();

private:
	int mHitPoints;
	int mAccuracy;
	int mExpReward;
	int mArmor;
	Weapon mWeapon;
}; 


And i want to make this:

m_Monsters = new Monster [m_MonstersNumber];

also i make this of coure:

Monster* m_Monsters;

But i'm getting error masage:

error C2061: syntax error : identifier 'Monster'

Why??

Please help me!!!!!!
closed account (zb0S216C)
Post the full line where the error occurs, and don't change it.

Wazzak
It give me this error:

1>e:\projects\monster2\game.cpp(351) : error C2061: syntax error : identifier 'Monster'

At this line:

m_Monsters = new Monster [m_MonstersNumber];
When you declare new instance of Monster class, you need to pass in essential parameters as declared in constructor of class
something like this:

 
Monster* m_Monsters = new Monster(hp, acc, xpReward, armor .... );


To be able to declare an array of Monster pointers, you probably want to do this:

in Monster class, add an empty constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Monster
{
public:
        //Constructor
	Monster(int hp, 
                     int acc, 
                     int xpReward, 
                     int armor, 
                     const char* weaponName, 
                     int lowDamage, 
                     int highDamage);
        
        //default constructor
        Monster();
....


Then in your main(), or wherever you wanna create the array.
do this:

1
2
3
4
5
6
7
8
9
//Example
const char thang[10] = "THANG DO";
int i = 1;
    
//This is to create new instances of Monster
Monster* pMonster = new Monster(1, 1, 1 ,1 , &thang[0], 1, 1);
   
//This will create an array of Monster type  
Monster* array = new Monster[i];
Look:

I make pointer of Monster in a other class CGame:

1
2
3
4
5
6
7
8
9
10
11
class CGame
{
private:
	ConLib* m_Console;
	int m_LastAction;

	int m_GameStatus;
	COORD m_Arena;
	CPlayer m_Player;
	Monster* m_Monsters;
	int m_MonstersNumber;


And in one func i want to make an array of Monsters:

m_Monsters = new Monster[m_MonstersNumber];

But i can't??????

I want to use this array of Monsters to get his position, hitpoints,......
Last edited on
closed account (zb0S216C)
Is the Monster class defined before or after CGame?

Wazzak
Last edited on
You cant make array of pointer to Monster class if there is a constructor which requires data to be passed in. or at least I think so.

That's why I suggested a default constructor.

closed account (zb0S216C)
ThangDo wrote:
"You cant make array of pointer to Monster class"

He/she isn't creating an array of pointers.

Wazzak
Is the Monster class defined before or after CGame?


Yes it is in another file and i inclode him.
Last edited on
OMG!!! i fixed my code!! :)

While i update my last project i forgot that i have a integer named Monster;

int Monster

And when i try

	m_Monsters = new Monster [m_MonstersNumber];


It give me a error!

Thanks everyone anyway! :D :)
Topic archived. No new replies allowed.