Jump to content

  • Log In with Google      Sign In   
  • Create Account

Component System - "no appropriate default constructor available"


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Sappharos   Members   -  Reputation: 140

Like
0Likes
Like

Posted 31 July 2011 - 11:01 AM

Basic C++ question, probably. Don't know whether it belongs in here, 'Game Programming' or 'For Beginners'...

I'm refactoring my game to use a component system. The code involved is:

entity.h

[source lang="cpp"]struct DrawComponent;struct DrawSystem;struct DrawComponent{ float size; unsigned char colour[4]; DrawComponent(DrawSystem *system);};struct DrawSystem{ std::list<DrawComponent*> components;};struct Mob{ DrawComponent drawComponent; Vector pos; Mob(DrawSystem *system);};[/source]

entity.cpp

[source lang="cpp"]DrawComponent::DrawComponent(DrawSystem *system){ system->components.push_back(this);}Mob::Mob(DrawSystem *system){ drawComponent = DrawComponent(system);}[/source]

I'm getting an error: "C2512: 'DrawComponent' : no appropriate default constructor available". I Googled it - a lot of people have had this problem before but the general advice is to create one or make sure it's not being called from anywhere. I don't want a default constructor; I want all instances of DrawComponent to be created with a DrawSystem* as an argument. And as far as I can see there's no need for one. Why is it complaining?

Thanks for your time.

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9668

Like
1Likes
Like

Posted 31 July 2011 - 11:04 AM

You're using assignment rather than initialization. Utilize the member initializer list:
Mob::Mob(DrawSystem *system) : drawComponent(system) 
{
}


#3 ncsu121978   Members   -  Reputation: 516

Like
0Likes
Like

Posted 31 July 2011 - 11:08 AM

struct Mob{
	 DrawComponent drawComponent; 
	 Vector pos;    
	 Mob(DrawSystem *system);
};

your DrawComponent drawComponent; there is also constructing an object and it will use the default constructor
maybe what you wanted was a pointer there instead?[/font][/color]

#4 Sappharos   Members   -  Reputation: 140

Like
0Likes
Like

Posted 31 July 2011 - 11:10 AM

You're using assignment rather than initialization. Utilize the member initializer list:

Mob::Mob(DrawSystem *system) : drawComponent(system) 
{
}


All fixed. It was unexpectedly simple, thanks a lot. :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS