I have a class, and i try to create an object of it but it give me error in compilation.
The error is something like "]b]undefined reference to 'Game::PowerUp::powerups[/b]", in constructor of Game::PowerUp.
The error also contain another 2 error : also are undefined reference but under ~PowerUp destructor.
it compiled well actually if i dont create an object of it in test.cpp(main program to create objects), but once i coded this "
PowerUp* power = new PowerUp(t, s, 4, 4);" in test.cpp, the compilation fails and give that that error message.
here is my code:
-
/*** Power Up class ***/
-
class PowerUp : public Health {
-
public:
-
PowerUp(
-
const Terrain* t,
-
Screen* s,
-
unsigned startX,
-
unsigned startY,
-
unsigned p = STD_POWER_UP
-
) : Health(t, s, startX, startY, p) {
-
powerups.insert(this);
-
}
-
virtual ~PowerUp() { powerups.erase(this); }
-
virtual void Use(Character* c) {
-
Health::Use(c);
-
delete this; // Powerups disappear once used
-
}
-
static PowerUp* Find(unsigned atX, unsigned atY) {
-
set<PowerUp*>::const_iterator it;
-
for (it = powerups.begin(); it != powerups.end(); it++) {
-
if ((*it)->xPos == atX && (*it)->yPos == atY) {
-
return (*it);
-
}
-
}
-
return 0;
-
}
-
virtual char GetSymbol() const { return '@'; }
-
private:
-
unsigned power;
-
static set<PowerUp*> powerups;
-