Default args for Class B an aggregate of Class A

K

Kiel

=====
My error is:
error C2512: no appropriate default constructor available

I'm trying to use the default args for class B when it is an aggregate
of class A. I could solve this problem with a point to B and
initialize it in the C'tor of A, but logically it makes sense as an
aggregate.

=====
Sample Code:

---- main.cpp ---
int main()
{
A foo; // <-- compiler error here
}

---- B.h ---
class B
{
public:
B();
// more stuff

protected:
m_number;
}
--- B.cpp ---
B:B(int num = 1) m_number(num)
{
// empty
}

--- A.h ---
class A
{
public:
A();
// more stuff

protected:
B obj;
}

--- A.cpp ---
A::A()
{
// empty
}
 
V

Victor Bazarov

Kiel said:
=====
My error is:
error C2512: no appropriate default constructor available

I'm trying to use the default args for class B when it is an aggregate
of class A.

'B' is not an aggregate of anything. Neither is 'A'. Your class 'A'
simply has a 'B' data member.
I could solve this problem with a point to B and
initialize it in the C'tor of A, but logically it makes sense as an
aggregate.

Again, none of your classes is an aggregate. Initialisation of data
members is done in the constructor initialiser list (read about them
in your favourite C++ book).
=====
Sample Code:
[..]

Don't post nonsense. Post _real_ code that generates the error
message you posted here. Whatever you posted wasn't C++, so many
errors... And condense everything into _a single_ source file, not
a bunch of them.

V
 
J

John Harrison

Kiel said:
=====
My error is:
error C2512: no appropriate default constructor available

I'm trying to use the default args for class B when it is an aggregate
of class A. I could solve this problem with a point to B and
initialize it in the C'tor of A, but logically it makes sense as an
aggregate.

=====
Sample Code:

---- main.cpp ---
int main()
{
A foo; // <-- compiler error here
}

---- B.h ---
class B
{
public:
B();
// more stuff

protected:
m_number;
}
--- B.cpp ---
B:B(int num = 1) m_number(num)
{
// empty
}

--- A.h ---
class A
{
public:
A();
// more stuff

protected:
B obj;
}

--- A.cpp ---
A::A()
{
// empty
}

The code you posted is clearly only an approximation to the real code
you have. Next time, don't type, use cut and paste. So taking a wild
guess I would say that you error is that you don't realise that default
arguments go in the constructor prototype not the constructor
definition. I.e. this is wrong

class B
{
B(int m);
};

B::B(int m = 1)
{
}

but this is right

class B
{
B(int m = 1);
};

B::B(int m)
{
}

Same goes for default arguments in any other context, put them in the
prototype not the defiition.

If that is not the answer then post the real code and you'll get the
answer in minutes.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top