468,253 Members | 1,241 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

Default args for Class B an aggregate of Class A

=====
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
}

Sep 3 '05 #1
2 1678
Kiel wrote:
=====
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
Sep 3 '05 #2
Kiel wrote:
=====
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
Sep 4 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gabriel Genellina | last post: by
49 posts views Thread by Mark Hahn | last post: by
1 post views Thread by Pablo | last post: by
11 posts views Thread by JustSomeGuy | last post: by
4 posts views Thread by Steven T. Hatton | last post: by
9 posts views Thread by Pierre Senellart | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.