473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1931
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
49
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon...
15
by: Stefan Behnel | last post by:
Hi! I'm trying to do this in Py2.4b1: ------------------------------- import logging values = {'test':'bla'} logging.log(logging.FATAL, 'Test is %(test)s', values)...
1
by: Pablo | last post by:
Friends, how can i get the name of the default printer??. I'm working with winforms. VB,C#, nevermind. Thanks a lot Pablo
11
by: JustSomeGuy | last post by:
I have a structure typedef struct { string a; string b; } atype; string ABC = ("123"); string DEF = ("456");
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
9
by: Pierre Senellart | last post by:
The C++ standard states (26.3.2.1), about std::valarray constructors: > explicit valarray(size_t); > > The array created by this constructor has a length equal to the value of > the argument....
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.