Driving me crazy!

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
Multibyte
Posts: 42
Joined: September 17th, 2013, 5:55 am
Location: USA

Driving me crazy!

Post by Multibyte » August 12th, 2018, 6:00 pm

So, an error which was driving me crazy has been solved finally. I wanted to summarize it here because it was weird and may happen to other noobs like me.

I've been working on Fart-Annoyed after watching the first two videos as Chili described. I was trying to create the array of Bricks and initialize them in Game.cpp to test it.

I kept getting this error (red line under the opening curly brace of Game.cpp's constructor where you initialize the array of bricks). Mouse-over read "the default constructor of 'Brick' cannot be referenced -- it is a deleted function".

This is the Brick class:

Code: Select all

#pragma once

#include "RectF.h"
#include "Colors.h"
#include "Graphics.h"

class Brick
{
public:
	Brick() = default;
	Brick(const RectF& rect_in, Color c_in);
	void Draw(Graphics& gfx) const;
private:
	RectF rect;
	Color c;
	bool isDestroyed = false;
};
I tried Google etc. with no success. Then I was commenting/un-commenting parts of code to localize what part of code was creating that error, and I saw another error message that said RectF was missing its' default constructor.

So, it was the RectF's missing default constructor that was causing the compiler to complain about Brick's constructor, probably because RectF is a parameter in Brick's constructor.

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

Re: Driving me crazy!

Post by albinopapa » August 12th, 2018, 7:00 pm

probably because RectF is a parameter in Brick's constructor.
Nope, it's because Brick has a RectF member, not because it was a part of the constructor paramter list.

In order for you to create a Brick using a default constructor, every member of Brick must be default constructable.

In order for a class or struct to be default constructable:
  • All members of the class must be default constructable
  • The class must have a constructor with an empty parameter list Brick(), Color(), RectF() defined
  • If no other constructors are defined, the class does not need to define a default constructor
The last one means, if there are absolutely no constructors, the class or struct is default constructable, provided all members are also default constructable.

It is acceptable to just declare the default constructor as = default as long as all members of the class/struct are default constructable.
Brick() = default;
RectF() = default;

Later on, you may find that there are cases you store objects that aren't default constructable which as you found out, makes your class unable to be default constructed. The way around this would be to actually define a default constructor that isn't declared = default; and assign default values to the members that are not otherwise default constructable.

In the case of the RectF, you could have made a default constructor for Brick that just assigned zeros to your RectF member.

Code: Select all

#pragma once

#include "RectF.h"
#include "Colors.h"
#include "Graphics.h"

class Brick
{
public:
   Brick() : rect(0.f, 0.f, 0.f, 0.f), c(Colors::Magenta){}
   Brick(const RectF& rect_in, Color c_in);
   void Draw(Graphics& gfx) const;
private:
   RectF rect;
   Color c;
   bool isDestroyed = false;
};
Of course, it would probably be better to just give RectF a default constructor.
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

User avatar
Multibyte
Posts: 42
Joined: September 17th, 2013, 5:55 am
Location: USA

Re: Driving me crazy!

Post by Multibyte » August 12th, 2018, 7:35 pm

Thanks for the explanation. 8-)

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Driving me crazy!

Post by chili » August 13th, 2018, 10:06 am

This problem catches a lot of beginners right in the nutz :lol:
Chili

Post Reply