expected unqualified-id before ‘int’

Slight compile problems. I'm Linux using g++

Here's what happens in terminal

john@john-laptop:/media/disk/Cannon$ make
g++ -Wall -O2 -c -o Enemy.o Enemy.cpp
Enemy.cpp:10: error: expected unqualified-id before ‘void’
Enemy.cpp:18: error: expected unqualified-id before ‘void’
Enemy.cpp:24: error: expected unqualified-id before ‘int’
Enemy.cpp:29: error: expected unqualified-id before ‘int’

make: *** [Enemy.o] Error 1

Here's the source code for Enemy.cpp

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 "Library.h" // Just std libs and SDL stuff and the H file below

ENEMY::ENEMY(ENEMY* enemy)
{
	setName(enemy->getName());
	setHealth(enemy->getHealth());
	setMoney(enemy->getMoney());
}

ENEMY::void createEnemy(char* name, int health, int money, int damage, int x, int y)
{
	setDamage(damage);
	setMoney(money);
	setHealth(health);
	setName(name);
}

ENEMY::void setPosition(int xPosition, int yPosition)
{
	xPos = xPosition;
	yPos = yPosition;
}

ENEMY::int getX()
{
	return xPos;
}

ENEMY::int getY()
{
	return xPos;
}


Source for Enemy.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Based off of Stats.h

class ENEMY: public STATS
{
	private:
			char damage;
			int armor;
			int xPos;
			int yPos;
	public:
		//ENEMY();
		ENEMY(ENEMY*);
		void createEnemy(char*, int, int, int, int, int);
		void setPosition(int xPosition, int yPosition);
		int getX();
		int getY();
};


and because this is related to stats i'll throw that in too

Stats.cpp

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
#include "Library.h" // Holds all our libraries

// This is based off Stats.h
// For more documentation see Stats.h

void STATS::setWeapon(char *name)
{
	weapon = name;
}

char* STATS::getWeapon()
{
	return weapon;
}

bool STATS::setName(char* newName)
{
	// If empty string return false
	if (strlen(newName) == 0)
		return false;
	
	// If string is bigger than allocated size (30) return false
	if (strlen(newName) > 32)
		return false;
		
	strcpy(name, newName); // If we get past the above apply name settings
	
	return true;
}

char* STATS::getName()
{
	return name;
}

void STATS::setMoney(int amount)
{
	money = amount;
}

int STATS::getMoney()
{
	return money;
}

void STATS::setHealth(int amount)
{
	health = amount;
}

int STATS::getHealth()
{
	return health;
}


and then there's

Stats.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// This class is the foundation for both the player & enemies
class STATS
{
	private:
	char name [50]; // A bit more allowing on the allocation
	
	// The rest are self explanatory if you've ever played a game
	int money;
	int health;
	char weapon[30];
	
	public:
	void setWeapon();
	char* getWeapon;
	bool setName(char* newName);
	char* getName();
	void setMoney(int amount);
	int getMoney();
	void setHealth (int amount);
	int getHealth ();
};


Can someone help me solve this, I've been on google for about 2 hours now, and I can't seem to figure this out. I have a feeling it's the way my classes are setup, but .. I'm not so sure..

Thanks in advance!!
void ENEMY::createEnemy(char* name, int health, int money, int damage, int x, int y)

you always need to write the type of the return value in front of the class name, like you did in Stats.cpp.
Thanks for the help, I see where your going with this, but the problem has gotten a bit worse.

I've added the void onto the line. And I still have the above error messages, plus this one

--> Enemy.cpp:3: error: return type specification for constructor invalid

All help is appreciated!
Bump (I hope that's allowed)
AleaIactaEst wrote:
you always need to write the type of the return value in front of the class name
Always but with the constructor, you can't specify a return type for it
Enemy.cpp:3: error: return type specification for constructor invalid
This is what the error says
Last edited on
Constructors don't return values, so do what AleaIactaEst said for every function except the constructor.

Also, I hope that Enemy.cpp somehow includes Enemy.h (it should be including it directly, not relying on
Library.h, if indeed that is gratuitously including it for you).

Topic archived. No new replies allowed.