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.
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.