Jump to content
  • Advertisement
Sign in to follow this  
Dom_152

No appropriate default constructor available

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK I have this annoying problem. When I compile I get this one annoying error: error C2512: 'DF::Video::DFTriangle' : no appropriate default constructor available Which points me to this code:
//Constructor
DFTriangleList::DFTriangleList()
{			
}


Now I really don't know what's wrong here. My DFTriangle class has the following constructor:
//Constructor
DFTriangle::DFTriangle(Video::DFVertex v1, Video::DFVertex v2, Video::DFVertex v3)
{
	m_vertices[0] = v1;
	m_vertices[1] = v2;
	m_vertices[2] = v3;
}


And my DFTriangleList only has an array of DFTriangle's which i don't touch in my constructor for DFTriangleList. Any ideas on what's going on.

Share this post


Link to post
Share on other sites
Advertisement
Since you didn't post enough code I can only assume but something is trying to use DFTriangle tri();

It's probably the array trying to fill itself in with empty triangles. You should be able to fix it by providing a default constructor or setting default values for the parameters in your current constructor, or use an array of pointers to triangles instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dom_152
And my DFTriangleList only has an array of DFTriangle's which i don't touch in my constructor for DFTriangleList. Any ideas on what's going on.


Well, you have an array of objects. C++ standard states that constructor is called for all objects before they are used. Array of objects is no different - default constructor (taking no arguments) will be called for each element of the array.

Simply add the constructor:

DFTriangle::DFTriangle()
{
}




And it should work.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dom_152
And my DFTriangleList only has an array of DFTriangle's which i don't touch in my constructor for DFTriangleList. Any ideas on what's going on.


There's your problem. TriangleList has an array of Triangles that must be constructed somehow, in this case by calling the default (parameterless) constructor. The compiler tries to do this for you, but can't because Triangle has no default constructor and the compiler isn't allowed to create one because Triangle already has the one you defined.

The solution is to write one yourself; if DFVertex has a suitable default constructor, an empty one should suffice:
DFTriangle::DFTriangle()
{
}


(BTW, why do you prefix your names with "DF" when you already have a namespace DF which is essentially the same thing?)

EDIT: Duh, of course DFVertex is a user-defined type, not a fundamental one. Edited the code to reflect this fact.

Share this post


Link to post
Share on other sites
Quote:
Original post by Paulius Maruska
Simply add the constructor:
*** Source Snippet Removed ***

And it should work.


Just adding that constructor could leave the object in an invalid state. It would be better to set the verticies in the default constructor so that you know they are set to something valid. Similar to what Sharlin has done.

Share this post


Link to post
Share on other sites
Quote:
Original post by tstrimp
Quote:
Original post by Paulius Maruska
Simply add the constructor:
*** Source Snippet Removed ***

And it should work.


Just adding that constructor could leave the object in an invalid state. It would be better to set the verticies in the default constructor so that you know they are set to something valid. Similar to what Sharlin has done.


The fact that it's an array (of vertices) tells us that the DFVertex has a default constructor. I'd rather have a default constructor called instead of assigning some, possibly bad, value to it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Paulius Maruska
The fact that it's an array (of vertices) tells us that the DFVertex has a default constructor. I'd rather have a default constructor called instead of assigning some, possibly bad, value to it.


You win!

Share this post


Link to post
Share on other sites
OK So I changed the constructor for DFTriangle to a blank one and it compiles fine. But now I loose the advantage of being able to set the triangles vertices in the constructor.

Here are my vertex classes:


class DFVertex
{
}; //Class DFVertex

//Just a simple coloured vertex.
class DFColouredVertex : public DFVertex
{
public:
//Vertex position
Core::DFVector m_position;

//Vertex colour
Core::DFVector m_colour;
}; //Class DFColouredVertex




My reasons for having a blank DFVertex class which DFColouredVertex inherits from is pretty much the same reasons you would use an interface. So a function can ask for a DFVertex but will accept any of the different vertex types that inherit from it. There is probably a nicer way to do it but I understand this way.

EDIT: I didn't explain that well but you should get the jist of it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!