why default constructor is not getting called

J

jagan

Hi All,
I am learning c++. While i was coding following sample
program i got stuck with this error.
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\calculator.o ..\src
\calculator.cpp
...\src\calculator.cpp: In function 'int main()':
...\src\calculator.cpp:34:7: error: invalid conversion from 'code (*)
()' to 'int' [-fpermissive]
...\src\calculator.cpp:14:2: error: initializing argument 1 of
'code::code(int)' [-fpermissive]
...\src\calculator.cpp:36:5: error: request for member 'display' in
'c1', which is of non-class type 'code()'
Build error occurred, build is stopped

What i understood from that error is line, code c1(); is
treated as a function which is returning a code object. Why default
constructor is not getting called in this case. If i remove "()" from
that line everyting works
fine as expected. Can some one explain me why default constructor is
not getting called like other constructors
which has aruguments, are called.

Below is full code:

#include <iostream>

using namespace std;


class code {
int id;
public:
code(){
id = 0;
}

code(int _id){
id = _id;
}

code(code &c){
id = c.id;
}

void display(){
cout << id << endl;
}
};

int main()
{
code c1();//default constructor not getting called
code c2(100);
code c3(c2);
code c4;

c4 = c1;//error
c1.display(); //error, not a code object
c2.display();
c3.display();
c4.display();
}

thanks.
 
A

Alf P. Steinbach

Hi All,
I am learning c++. While i was coding following sample
program i got stuck with this error.
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\calculator.o ..\src
\calculator.cpp
..\src\calculator.cpp: In function 'int main()':
..\src\calculator.cpp:34:7: error: invalid conversion from 'code (*)
()' to 'int' [-fpermissive]
..\src\calculator.cpp:14:2: error: initializing argument 1 of
'code::code(int)' [-fpermissive]
..\src\calculator.cpp:36:5: error: request for member 'display' in
'c1', which is of non-class type 'code()'
Build error occurred, build is stopped

What i understood from that error is line, code c1(); is
treated as a function which is returning a code object. Why default
constructor is not getting called in this case. If i remove "()" from
that line everyting works

That's because anything that *can* be parsed as a function declaration,
is parsed as a function declaration.

It's known as C++'s "most vexing parse".

Anyway you got it right.

An alternative is to write

T o = T();

Formally and syntactically this is a copy initialization. But in
practice the compiler will optimize it down to direct
default-initialization. The only problem is that technically T must then
have an accessible copy constructor (e.g., a std::eek:stringstream doesn't).


Cheers & hth.,

- Alf
 
J

Jorgen Grahn

Hi All,
I am learning c++. While i was coding following sample
program i got stuck with this error. ....
#include <iostream>

using namespace std;


class code {
int id;
public:
code(){
id = 0;
}

code(int _id){
id = _id;
}

These are much better written

code(int id=0) : id(id) {}
code(code &c){
id = c.id;
}

Same here, and please read up on 'const':

code(const code& other) : id(other.id) {}
void display(){
cout << id << endl;
}

- There's a 'const' missing here too
- an 'ostream << code' operator is really much more useful
in the long run
- don't force a newline and a stream flush into the displaying;
leave that choice to the caller

Good luck, and I hope you will enjoy C++ programming for many years.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top