From: denis_browne on
When writing the following code:

class Base
{
Base(const Base &rhs) {}
~Base();
};

void f() {
Base b;
};

The compiler shouts: no appropriate default constructor available

But when changing it to:

void f() {
Base b();
};

it compiles. Why?


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

From: Victor Bazarov on
denis_browne(a)hotmail.com wrote:
> When writing the following code:
>
> class Base
> {
> Base(const Base &rhs) {}
> ~Base();
> };
>
> void f() {
> Base b;
> };
>
> The compiler shouts: no appropriate default constructor available

Yes, since you defined the copy-c-tor, compiler does not generate
the default c-tor.

> But when changing it to:
>
> void f() {
> Base b();
> };
>
> it compiles. Why?

Because in the latter case there is no object of type 'Base' to
be constructed.

V
--
Please remove capital As from my address when replying by mail



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

From: mdlinux7 on
> void f() {
> Base b();
> }

This compiles because this is treated as a function declaration bu
compiler which takes void as an argument and returns a class Base
object.

Base b() ; // It is not a instantiation of an object via default
constructor.

The above code will give linking error if this function defination is
not provided.

Regards
Dinesh

denis_browne(a)hotmail.com wrote:
> When writing the following code:
>
> class Base
> {
> Base(const Base &rhs) {}
> ~Base();
> };
>
> void f() {
> Base b;
> };
>
> The compiler shouts: no appropriate default constructor available
>
> But when changing it to:
>
> void f() {
> Base b();
> };
>
> it compiles. Why?

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

From: Daniel T. on
In article <1140077839.789605.237410(a)g44g2000cwa.googlegroups.com>,
denis_browne(a)hotmail.com wrote:

> When writing the following code:
>
> class Base
> {
> Base(const Base &rhs) {}
> ~Base();
> };
>
> void f() {
> Base b;
> };
>
> The compiler shouts: no appropriate default constructor available
>
> But when changing it to:
>
> void f() {
> Base b();
> };
>
> it compiles. Why?

In the latter case, you are declaring a function named 'b' that takes no
parameters and returns a 'Base'.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.

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

From: Thomas Maeder on
denis_browne(a)hotmail.com writes:

> class Base
> {
> Base(const Base &rhs) {}
> ~Base();
> };
>
> void f() {
> Base b;
> };
>
> The compiler shouts: no appropriate default constructor available
>
> But when changing it to:
>
> void f() {
> Base b();
> };
>
> it compiles. Why?

Because

Base b();

doesn't define an object, but declares a function.

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