Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Andre the Giant

I don't WANT a default constructor.

This topic is 5804 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

Ive created a class called Color that Im going to use to represent the R, G and B values in OpenGL color functions. I also have a class called Ball, and Im going to have a lot of balls in my OpenGl program ( yea, im sort of a newbie! ). I want each ball to have a different color, so the Ball class has a member variable declared like so: private: Color color; My Ball constructor accepts a color object and saves it like so: Ball::Ball(Color c) { color = c; Also, my Color object has 2 different constructors, each of which accept parameters (so i DON''T have a default constructor). I keep getting compile errors saying that "there is no appropriate default constructor available" for the Color class. It points to the Ball constructor as the culprit. Does this mean i have to define a default constructor for the Color class? Because I dont want to! Anytime a color object is created, I want it to be a specific chosen color. It doesnt make sence to me that I should be forced to provide a default constructor. I shouldnt have to change the design of my program for a strange syntax quirk. I must be doing something wrong. Can someone tell me what? P.S. I Was going to post some code, but I have a lot of it, and im not sure exactly what code is causing problems, So Ill just wait and see if anyone can solve this without code first. Thanks a lot in advance! Its not my fault I''''m the biggest and the strongest; I don''''t even exercise.

Share this post


Link to post
Share on other sites
Advertisement
You must have a default constructor. No way around it. Have it do nothing, or, do it like you should and make it initialize all member variables in the class.

[edit]BTW, it is possible to have more than one constructor, through function overloading. Make your declaration look like this:

Ball();
Ball(Color c);

then in your code:

Ball::Ball()
{
memset (color, 0, sizeof(color));
}
Ball::Ball(Color c)
{
color = c;
}

[edited by - Neosmyle on August 29, 2002 8:04:43 PM]

Share this post


Link to post
Share on other sites
AFAIK, you don't need a default constructor, but you have to make sure that every instance of Ball has arguments for the constructor, or else your compiler will complain. For example, you can't have

Ball b; //The compiler should flag this error

Furthermore, if your Color objects don't have a default constructor, your Ball constructor should not be

Ball::Ball(Color c) {
color = c;
...

but rather

Ball::Ball(Color c) : color(c){...}

Which is, BTW, the preferred method even if Color had a default constructor.

Cédric

EDIT: Clarified stuff and fixed grammar.

[edited by - cedricl on August 29, 2002 8:35:13 PM]

Share this post


Link to post
Share on other sites
I''m very probably wrong here, but isn''t it quicker to do...


  
class Ball
{
public:

Ball(Colour col) : m_Colour(col) {}

private:

Colour m_Colour;
};


...since doing this means that it won''t make a temporary copy on the stack of the Colour object that you pass into the contructor? Or am I remembering things from a long time ago when compilers weren''t quite as optimising as they are nowadays?

Share this post


Link to post
Share on other sites
Your given constructor could also supply a default argument and thereby serve as a default constructor...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you don''t have to have a default constructor but if you don''t you have to initialize everything, for example you have a vertex class with a color as a member:

  
struct Vertex
{
Color tint;
Vertex();
};

//ok so now you have to implement the contructor,

you try this but it is wrong

Vectex::Vertex()
{
tint=Color(255,255,255);
}

//that''s because tint has to have a legal value before

//you get into the body of the constructor, so use this instead


Vertex::Vertex() : tint(255,255,255)
{
}

Share this post


Link to post
Share on other sites
quote:
Original post by Andre the Giant
I keep getting compile errors saying that "there is no appropriate default constructor available" for the Color class. It points to the Ball constructor as the culprit.

Does this mean i have to define a default constructor for the Color class? Because I dont want to! Anytime a color object is created, I want it to be a specific chosen color. It doesnt make sence to me that I should be forced to provide a default constructor. I shouldnt have to change the design of my program for a strange syntax quirk. I must be doing something wrong.

Can someone tell me what?



Just make the default constructor, damn it...


class Color
{
Color() {} // default ctor == do nothing
...
};

Share this post


Link to post
Share on other sites
There seem to be two issues here:

First issue: Can you have a class without a default constructor? The answer is yes, like this:


    
class Ball {
private:
Ball(); //declare, but don't define!

Color col;
public:
Ball(const Color& c) : col(c) { }
}:


Provided you do not define a default constructor for Ball, the class will not have a default constructor. The same can be done for the copy constructor if you don't want that (and assignment as well, I'm certain).

Second issue: The error you quote is there is no appropriate default constructor available for the Color class . Note that this is talking about constructors on the Color class, not the Ball class. So the compiler is telling you that your Color class has no default constructor, but you are attempting to use it anyway.

Look to see where you are instatiating the Color class and figure out whether you need to provide parameters upon its construction, or provide a default constructor for Color.


---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!


[edited by - mossmoss on August 29, 2002 9:21:18 PM]

Share this post


Link to post
Share on other sites
Try something like this Andre

Ball(Color c = Color(255,255,255)) : color(c) {}


...
You don''t have to make the default ctor private, nor use a declare but don''t define trick to not have one. (You use that to hide copy ctor''s.)

This class has no default ctor:

class Object
{
public:
Object(int i) {}
};

A default ctor is made for you automatically iff (that''s if and only if) you do not define any other ctor''s.

Share this post


Link to post
Share on other sites

  • 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!