473,322 Members | 1,232 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,322 software developers and data experts.

no appropriate default constructor available

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?

Feb 16 '06 #1
3 4119
de**********@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?


Base b(); declares a function 'b' taking no arguments and returning
an object of type 'Base'. Not so many days ago I read a nice 'rule of
thumb', if you may call it that: If it looks like a function
declaration, it is a function declaration. (Or something along the
lines)

hth
--
jb

(reply address in rot13, unscramble first)
Feb 16 '06 #2
<de**********@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com
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?


Because in the second case you haven't declared an instance of Base. You
have instead declared a function that takes no arguments and returns a Base
object.
--
John Carson
Feb 16 '06 #3
de**********@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?


The compiler is correct. A default constructor either takes no
parameters or has default values for all of the parameters, hence
enabling the default constructor call with no parameters or using the
already defined default parameters. Hence any one of the following is a
valid default constructor:

Base() { .... }
Base(int n = 7) { .... }
Base(char *name = NULL, unsigned id = 0) { .... }

JB
Feb 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: news.microsoft.com | last post by:
BUG: struct default constructor is not displayed on intellisense yet it is available, BUG Make a struct type with a parameterized constructor. Try to construct the type in the IDE and...
4
by: Duy Lam | last post by:
The compiler is complaining about "no appropriate default constructor available" when I reference a subclass. The basic setup is that i have a class Test and a subclass called TestKid. I want to...
4
by: stephane | last post by:
I can't make this work it says I dont have a default constructor available. Can someone tell me what's wrong with this please? #include <iostream> using namespace std; #include "points.h" ...
5
by: Ook | last post by:
Here is my code, can some kind soul tell me what I'm doing wrong, or why I get this compile error, and maybe what to do to prevent it? I think I must be missing some fundamental concept here, or...
3
by: Stanislav Simicek | last post by:
Hello, I'm trying to implement "Null-Field" feature (like Stream::Null) in MC++, but I am not able to initialize static member properly due to compiler error C2512 (no appropriate default...
0
by: WithPit | last post by:
I have some problems with the instantiating new objects. For example I have the following code //Headerfile (Texture1D.h> #pragma once #using <mscorlib.dll> #include <osg/Texture1D> #include...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
2
xarzu
by: xarzu | last post by:
What causes the no appropriate default constructor available linker error when I do have an constructor defined in the header file?
3
by: Rahul | last post by:
Hi Everyone, I have the following code and the compiler complains that there isn't any default constructor available, class C { private: C() {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.