0

I'm wondering why I need to declare a default constructor in this case. For one thing doesn't the compiler do that automatically if i leave it out? And regardless, I still don't see why its necessary. Also, I get the error even if I omit 'obj_B = origin.obj_B;'

class B
{
public:
    bool theArray[5] ;

    B(bool x) {theArray[1] = x;};
    //B(){};    
};

class A
{
public:
    B obj_B;

    A() : obj_B(1) {};
    A(A const &origin) {obj_B = origin.obj_B;}; //error:no matching function for call 
                                                      //to B::B()
}; 

int main () 
{
    std::vector <A> someAs;
    for(int q=0;q<10;q++)
        someAs.push_back(A());

    for(int q=0;q<10;q++)
        std::cout << someAs[q].obj_B.theArray[1] << std::endl;
}

4 Answers 4

6

The compiler only creates a default constructor if you don't specify an alternate constructor.

Because you made:

B(bool x) {theArray[1] = x;}

The default constructor will not be created for you.

The specific error you're getting is because A(A const &origin) doesn't specify the constructor to use for obj_B explicitly.

The following code would work:

A(A const &origin) : obj_B(1) {obj_B = origin.obj_B;}

By the way, you don't need a trailing semicolon on a function definition.

Sign up to request clarification or add additional context in comments.

5 Comments

@ Wheatevo Cool. I know its not an issue in this code, but is there any reason that using a default copy constructor or default assignement-operator could fail to copy the contents of a non-dynamically allocated array (eg theArray5)?
In the vast majority of cases, the default copy constructor, default assignment operator, and default destructor will be sufficient if you don't use dynamically allocated memory.
Anyway, there's no reason that the default copy or assignment operator would fail to copy the contents of a typical array. You only need to make your own copy constructor if you're dealing with dynamically allocated memory or something like reference counters. The default copy will automatically copy each member variable of one object to another (excluding static members).
@Wheatevo Alright. That's what everyone's been saying. But that's the problem I've been having and I can't for the life of me figure out what's wrong. What about the tiny minority of cases?
I would suggest posting a new question with the code you're having trouble with :)
2

If you don't define any ctors for a class, the compiler will synthesize a default constructor. If you define another constructor though (e.g., one that takes an argument) then the compiler does not synthesize one for you, and you have to define one yourself.

In case you care, C++ 0x adds an "=default;" declaration to tell the compiler to provide a ctor that it would have provided by default, even if you have defined another ctor.

Comments

2

To define a copy constructor for A that does not require a default constructor for B, use member initializer syntax:

class A {
public:
    A(A const& origin) : obj_B(origin.obj_B) {}
    //...
};

Comments

0

To make one final point...

Assuming you hadn't defined a non-default constructor, failing to define a default constructor would've resulted in the elements of theArray[] being undefined. That is a bad habit to get into usually leading to bugs down the road.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.