422,734 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,734 IT Pros & Developers. It's quick & easy.

create object by using new, error in compilation

100+
P: 254
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:
Expand|Select|Wrap|Line Numbers
  1. /*** Power Up class ***/
  2.     class PowerUp : public Health {
  3.     public:
  4.         PowerUp(
  5.             const Terrain* t,
  6.             Screen* s,
  7.             unsigned startX,
  8.             unsigned startY,
  9.             unsigned p = STD_POWER_UP
  10.         ) : Health(t, s, startX, startY, p) {
  11.             powerups.insert(this);
  12.         }
  13.             virtual ~PowerUp() { powerups.erase(this); }
  14.             virtual void Use(Character* c) {
  15.                 Health::Use(c);
  16.                 delete this;    // Powerups disappear once used
  17.             }
  18.             static PowerUp* Find(unsigned atX, unsigned atY) {
  19.                 set<PowerUp*>::const_iterator it;
  20.                 for (it = powerups.begin(); it != powerups.end(); it++) {
  21.                     if ((*it)->xPos == atX && (*it)->yPos == atY) {
  22.                         return (*it);
  23.                     }
  24.                 }
  25.                 return 0;
  26.             }
  27.             virtual char GetSymbol() const { return '@'; }
  28.     private:
  29.         unsigned power;
  30.         static set<PowerUp*> powerups;
  31.  
Jan 4 '07 #1
Share this Question
Share on Google+
2 Replies


Ganon11
Expert 2.5K+
P: 3,652
Have you initialized the Set powerups in your .cpp file?
Jan 4 '07 #2

100+
P: 254
thanks Ganon11,

i have settled this problem 5 minutes ago.

yes, you're right. I didn't yet initialise the powerups static variable.

Thanks.
Nicky Eng.
Jan 4 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.