Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post Posted: September 24th, 2017, 7:36 am 
 

Joined: September 24th, 2017, 3:23 am
Posts: 19
Hi, I'm having a problem with my array declaration. I'm trying to declare an array of "Enemy". Enemy is a class that i made to represent enemies. The compiler is complaining about an operator that is deleted. I don't get this at all. I've already updated my visual studio to the latest version. Here's my code.

Code:
Enemy.h
class Enemy
{
private:
   int _x;
   int _y;
   Graphics& _gfx;
public:
   Enemy() = default;
   Enemy(int x, int y, Graphics& gfx);
};

Enemy.cpp
Enemy::Enemy(int x, int y, Graphics& gfx)
   :
   _gfx(gfx)
{
   _x = x;
   _y = y;
}

Game.h
class Game
{
public:
   Game( class MainWindow& wnd );
   Game( const Game& ) = delete;
   Game& operator=( const Game& ) = delete;
   void Go();
private:
   void ComposeFrame();
   void UpdateModel();
   /********************************/
   /*  User Functions              */
   
   /********************************/
private:
   MainWindow& wnd;
   Graphics gfx;
   /********************************/
   /*  User Variables              */
   static constexpr int maxEnemy = 100;
   Enemy enemy[maxEnemy];
   /********************************/
};


Game.cpp
Game::Game( MainWindow& wnd )
   :
   wnd( wnd ),
   gfx( wnd )
{
   for (int i = 0; i < maxEnemy; i++)
   {
      enemy[i] = Enemy(20, 20, gfx);
   }

}

void Game::Go()
{
   gfx.BeginFrame();   
   UpdateModel();
   ComposeFrame();
   gfx.EndFrame();
}

void Game::UpdateModel()
{

}

void Game::ComposeFrame()
{

}


The equal (=) sign inside the loop has red underline. Also on the square bracket ({) of the implementation of the Game constructor.

I'm really stuck here. Please help.

Edited by albinopapa:
Put code between code tags:
Code:
[code][/code]


Top 
 Profile  
Reply with quote  
 Post Posted: September 24th, 2017, 7:50 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2908
Location: Oklahoma, United States
Code:
   Graphics& _gfx;

Here you are trying to store a reference to the Graphics object, so Enemy = default; means nothing because you have to initialize a reference during construction. Since you can't have a default constructor with a stored reference, your array can't be instantiated because it wants to default construct them first before you ever try assigning to them using the second constructor Enemy(x,y,gfx).

Your best bet for this would be to just pass in the graphics reference to the draw function OR make Enemy store a Graphics * ( graphics pointer ). References can't be initialized after construction and can't be reassigned, but pointers can.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post Posted: September 24th, 2017, 8:14 am 
 

Joined: September 24th, 2017, 3:23 am
Posts: 19
I get it now. Thank you so much for pointing that out albinopapa.

offtopic: people here have sexy names.


Top 
 Profile  
Reply with quote  
 Post Posted: September 24th, 2017, 5:43 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2908
Location: Oklahoma, United States
We all try to please the chili, gotta get noticed lol. Yeah, I like the names around here.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 4 posts ] 

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron