Array of Class Declaration

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Empirean
Posts: 26
Joined: September 24th, 2017, 3:23 am

Array of Class Declaration

Post by Empirean » September 24th, 2017, 7:36 am

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: Select all

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: Select all

[code]
[/code]

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Array of Class Declaration

Post by albinopapa » September 24th, 2017, 7:50 am

Code: Select all

   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

Empirean
Posts: 26
Joined: September 24th, 2017, 3:23 am

Re: Array of Class Declaration

Post by Empirean » September 24th, 2017, 8:14 am

I get it now. Thank you so much for pointing that out albinopapa.

offtopic: people here have sexy names.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Array of Class Declaration

Post by albinopapa » September 24th, 2017, 5:43 pm

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

Post Reply