Fundamentally C++ Headaches...

Started by
4 comments, last by Zahlman 15 years, 8 months ago
Ok.. here's the problem. I have this object in C++ called Pet and there is NO default constructor for it bascially because it needs to implement a parent object which takes in a parameter and if you create a default constructor for it, there will be a lot of messy errors which I don't understand. So when i try to create an array/vector for this object, they say that the object has no default constructor... So what should I do now? Urgent Urgent!
Advertisement
Have your array/vector contain a set of pointers to Pets, instead of Pet objects themselves.
Mike Popoloski | Journal | SlimDX
Assuming you are talking about a C++ Standard Library's vector (std::vector), you can provide your vector a default object to copy-construct from.
http://www.cplusplus.com/reference/stl/vector/

maybe you're looking for vector::insert or vector::push_back?
If you post some code, it will be much easier to spot the error. :)
Quote:Original post by Gemira
Ok.. here's the problem.

I have this object in C++ called Pet and there is NO default constructor for it bascially because it needs to implement a parent object which takes in a parameter and if you create a default constructor for it, there will be a lot of messy errors which I don't understand.

So when i try to create an array/vector for this object, they say that the object has no default constructor... So what should I do now?

Urgent Urgent!


First off, I really doubt your problem can really be that urgent.

Anyway, it seems to me that the obvious answer is "understand the errors".

Here is the most common situation that sounds like what you're describing. I'm not psychic - I can try, but it would be much better if you would just post the problematic code and errors.

In C++, objects of a derived class include an instance of the base ("parent" as you put it) class as part of the memory layout. This is how derived classes get access to all the base members. In order to construct any class in C++, all its members and bases need to get constructed.

Whenever you have a class and you don't define any constructors for it, the compiler will automatically make a default constructor. If you define one, the default constructor is removed. This is a safety feature: it doesn't make sense to default-construct some kinds of objects. If you need a no-argument constructor in addition to the one you defined, you need to add it back.

When an object is constructed, there are actually steps that take place before any of the code in the constructor is run. That is initialization: basically, constructors are called for the members and bases. Which constructor? Well, if you don't specify otherwise, it will try to use the default constructor - that's what "default" means, after all. (For primitive types, the compiler pretends they have a "constructor" which sets the value to zero.) And if the default constructor isn't there, then the compiler will report an error: you're trying to initialize something in a "default" way that was specifically designed not to be initialized that way. So in that case, you'll need to say which constructor to call, and what arguments to pass to it. This is done with the initialization list. For example:

class Base {  int i;  // Let's give Base a constructor, to remove the default constructor.  Base(int i): i(i) {}  // The :i(i) part is an initialization list for the Base constructor.  // 'i' is a member of Base, so we can say here how to initialize it.  // Normally it would just get zero-initialized (except in a few rare cases  // designed for C backwards-compatibility), but here we assign it the value  // of the passed-in parameter.};// Now let's derive from our base:class Derived {  // And define a constructor. We need to tell it to call the (int) constructor  // of the base class, and tell it what value to use. To make things a little  // more annoying, the user of the Derived class is going to pass a value  // that's one less than what we want to use in the Base. But that's not a  // problem: we can use expressions (not just variables) to initialize.  Derived(int i_less_one): Base(i_less_one + 1) {}};

This topic is closed to new replies.

Advertisement