why default constructor is not getting called

Discussion in 'C++' started by jagan, Dec 11, 2011.

  1. jagan

    jagan Guest

    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.
    jagan, Dec 11, 2011
    #1
    1. Advertising

  2. On 11.12.2011 06:57, jagan wrote:
    > 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
    Alf P. Steinbach, Dec 11, 2011
    #2
    1. Advertising

  3. jagan

    Jorgen Grahn Guest

    On Sun, 2011-12-11, jagan wrote:
    > 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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Dec 11, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert
    Replies:
    6
    Views:
    527
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,848
    Smokey Grindel
    Dec 2, 2006
  3. Alex Vinokur
    Replies:
    9
    Views:
    411
    Thomas J. Gritzan
    Aug 10, 2006
  4. Replies:
    9
    Views:
    1,840
    Alf P. Steinbach
    Oct 14, 2006
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,226
Loading...

Share This Page