Public Group

# No Default Constructor

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In the following code:
AnimatedTile::AnimatedTile(Animation animation);
I'm getting the error: "Animation - no appropriate default constructor available." There is no default constructor for Animation (at least not written by me). I don't want there to be one, because you shouldn't be able to make an Animation without at least one Image in it. Do I not have that option? And why would this error appear here, anyway? An already-constructed Animation should be passed in to this constructor, so if the problem is going to occur, it shouldn't occur here anyway, right? On an unrelated note, how do I make a global constant in a multi-file project? I've tried just const int TILE_SIZE = 32; at the top of main.cpp, but no other file seems able to find it, and I'm trying to avoid just typing 32 every time. Thanks for helping...

##### Share on other sites
That may be enough code to tell you what your problem is, but I'm not smart enough. At least show all constructors you have for Animation. I'm guessing you need a copy constructor.
Animation::Animation(const Animation &animation){  this->member1 = animation.member1;  // etc...  // or overload operator= and use it here like this:  // *this = animation;}

Problem 2:

Read this a few times. Basically, you'll need to add to make a header file and include that in every file that needs that value. Don't want to do that? Pick a different language. That said, I bet if you wrote your program a little different you would only need to use that constant in one or two files.

Hope that helps.

##### Share on other sites
Any call to the AnimatedTile constructor is going to invoke the copy constructor for the Animation parameter. This is because you are passing this argument by value. I assume this is not what you want.

If you do not want anyone (including yourself) to use any of the default methods then declare them private.

##### Share on other sites
I have defined both a copy constructor and an assignment operator for class Animation, as follows:

Animation& Animation::operator =(const Animation& rv){	this->m_Images = rv.m_Images;	this->m_CurrentImage = rv.m_CurrentImage;	this->m_CurrentIndex = rv.m_CurrentIndex;	this->m_Delay = rv.m_Delay;	this->m_Pos = rv.m_Pos;	this->m_Waited = rv.m_Waited;	return *this;}Animation::Animation(const Animation &animation){	*this = animation;}

There is only one constructor for class Animation:

Animation::Animation(std::vector<Image> images, double delay){	m_Images = images;	m_Delay = delay;	m_Waited = 0.0;	m_CurrentImage = m_Images.at(0);	m_CurrentIndex = 0;}

I changed the argument to AnimatedTile's constructor to const Animation& animation, with no change in the error message.

I feel that there's some fundamental gaps in my knowledge of this particular subject... can anyone recommend some research material to me? I didn't see anything promising in the articles section. Books would be fine - I have a very excellent library two blocks from me - but online would be easier.

And just so we're clear - I'd still like to know how to solve the immediate problem, as well as the theory behind why it's happening.

Thanks for the help thus far.

##### Share on other sites
Did you make your Animation constructor explicit?

##### Share on other sites
I'm not sure what that means... I've posted the constructor, which is the only one that exists... Its definition in the class header file goes like this:

class Animation{...public:Animation(std::vector<Image> images, double delay);...}

##### Share on other sites
AnimatedTile::AnimatedTile(Animation animation);

That line of code is very bizarre, actually. What do you think it does?

##### Share on other sites
Quote:
 Original post by nobodynewsAnimatedTile::AnimatedTile(Animation animation);That line of code is very bizarre, actually. What do you think it does?

Um... It's the declaration (well actually definition, I snipped the body) of a constructor for class AnimatedTile which takes an Animation (which is referred to in said snipped body as animation) as an argument.

... Right?

I'll bet $20 and a chocolate bar that you're doing something like thi in the AnimatedTile class: class AnimatedTile{ Animation anim;public: AnimatedTile(Animation anim) { this -> anim = anim; }} This cannot work, because Animation has no default constructor, and therefore, you have to provide constructor arguments for your AnimatedTile::anim member in the AnimatedTile constructor. This is done using an initializer list: AnimatedTile(Animation anim) : anim(anim) {} #### Share this post ##### Link to post ##### Share on other sites *Gives 20$ and a chocolate bar to ToohrVyk, then hangs his head in shame.*

That, of course, was the problem. Thanks very much for all your help, everyone, and hopefully I'll never do this again...

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 15
• 13
• 9
• 12
• 10
• ### Forum Statistics

• Total Topics
631440
• Total Posts
3000080
×