From: kiran on
Please look at the following lines

class A {
public:
int i;
A(int j) {
i = j;
cout<<endl<<" A's constructor" ;
}
};

class B {
public:
int k;
A a;
B( ) {
k = 0;
cout<<endl<<" Bs constructor" ;
}
} ;

Now, when I declare an object of B, I face an error, as there is no
matching call to A's constructor. What is the work around?
I have tried different manipulations (like A a(3), writing a(3) inside
B's constructor etc. and none of them helped).

thanks and regards,
kiran

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: d04rp on
> Please look at the following lines
>
> class A {
> public:
> int i;
> A(int j) {
> i = j;
> cout<<endl<<" A's constructor" ;
> }
>
> };
>
> class B {
> public:
> int k;
> A a;
> B( ) {
> k = 0;
> cout<<endl<<" Bs constructor" ;
> }
>
> } ;
>
> Now, when I declare an object of B, I face an error, as there is no
> matching call to A's constructor. What is the work around?
> I have tried different manipulations (like A a(3), writing a(3) inside
> B's constructor etc. and none of them helped).

As soon as you declare a constructor for a class the compiler will not
syntezie any (default) constructor for you.
So in order to correct this code you need to add the following to the
initializer list a(3).
so change;

B( ) {

to

B() : a(3) {

HTH
Roger Schildmeijer


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: David Pol on
On 29 mar, 20:44, kiran <kiran.tange...(a)gmail.com> wrote:
> Please look at the following lines
>
> class A {
> public:
> int i;
> A(int j) {
> i = j;
> cout<<endl<<" A's constructor" ;
> }
>
> };
>
> class B {
> public:
> int k;
> A a;
> B( ) {
> k = 0;
> cout<<endl<<" Bs constructor" ;
> }
>
> } ;
>
> Now, when I declare an object of B, I face an error, as there is no
> matching call to A's constructor. What is the work around?
> I have tried different manipulations (like A a(3), writing a(3) inside
> B's constructor etc. and none of them helped).
>
> thanks and regards,
> kiran

Hello,

Your code does not compile because there is no default constructor
available for A. And such a constructor is needed in order to properly
default-construct the A member in B's default constructor. You can
either provide a default constructor for A (that is, a constructor
that can be called with no arguments):

class A
{
public:
A(int j = 0) : i(j) { }
};

Or explicitly call you current constructor for A from the
initialization list in B's default constructor:

class B
{
public:
B() : a(0) { }
};

Regards,
David

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Timo Geusch on
kiran <kiran.tangeeda(a)gmail.com> writes:

<snip code example>

> Now, when I declare an object of B, I face an error, as there is no
> matching call to A's constructor. What is the work around?
> I have tried different manipulations (like A a(3), writing a(3) inside
> B's constructor etc. and none of them helped).

You may want to try writing B's constructor along the following lines:

B::B() : a(3) {
... rest of the construction goes here ...
}

The problem is that you are not declaring a constructor for A that
doesn't take any parameters, so the compiler can't default construct an
A object.

Also, more on a stylistical note - in you example all your data was
public, which is generally *not* a good idea in C++ (or for that matter,
in any OO language).

--
Timo Geusch
Codesmith Consulting Ltd
The lone C++ coder's blog: http://codeblog.bsdninjas.co.uk/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 29 Mrz., 20:44, kiran <kiran.tange...(a)gmail.com> wrote:
> Please look at the following lines
>
> class A {
> public:
> int i;
> A(int j) {
> i = j;
> cout<<endl<<" A's constructor" ;
> }
> };
>
> class B {
> public:
> int k;
> A a;
> B( ) {
> k = 0;
> cout<<endl<<" Bs constructor" ;
> }
>
> } ;
>
> Now, when I declare an object of B, I face an error, as there is no
> matching call to A's constructor. What is the work around?
> I have tried different manipulations (like A a(3), writing a(3) inside
> B's constructor etc. and none of them helped).

C++ provides the concept of constructor initializers,
which allow you to do that:

class B {
public:
int k;
A a;
B( ) : k(0), a(3) {
cout<<endl<<" Bs constructor" ;
}
};

To demonstrate it's general usefulness I also moved
the initialization of member k onto the same list (You
could do the same with member i in class A).

This list is also *required*, if you need to initialize
other entities, like base classes with no default c'tor,
references, or (non-static) const data members.

HTH & Greetings from Bremen,

Daniel Kr�gler




--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 |  Next  |  Last
Pages: 1 2 3 4
Prev: Polymorphism question
Next: Using unique with std::map