Thread: Where to define derived class consructors ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Where to define derived class consructors ?

    I thought they, as for general member functions can be defined either inside the class as an inline or in a .cpp file. But I'm totally confused by the following error I'm getting.

    What works:
    Code:
    #ifndef KEYWORD
    #define KEYWORD
    #include "token.h"
    class Keyword : public token
    {
    public:
        Keyword(std::string input):token(input){/*....*/};
    };
    
    #endif
    What doesn't work :
    .h
    Code:
    #ifndef KEYWORD
    #define KEYWORD
    #include "token.h"
    class Keyword : public token
    {
    public:
        Keyword(std::string input):token(input);
    };
    
    #endif
    .cpp
    Code:
    #include "Keyword.h"
    Keyword::Keyword(std::string input):token(input)
    {
        
    }
    Error:
    Quote Originally Posted by g++-4.6
    Code:
    $make
    g++ Keyword.cpp -c -g -std=c++0x -o keyword.o
    In file included from Keyword.cpp:1:0:
    Keyword.h: In constructor ‘Keyword::Keyword(std::string)’:
    Keyword.h:7:43: error: expected ‘{’ at end of input
    Keyword.cpp: At global scope:
    Keyword.cpp:2:1: error: redefinition of ‘Keyword::Keyword(std::string)’
    Keyword.h:7:5: error: ‘Keyword::Keyword(std::string)’ previously defined here
    make: *** [keyword.o] Error 1
    What am I missing ?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    When you use the constructor initialisation list that doesn't mean you can forego having a function body. You still need empty curly braces to give the constructor a body.
    That said, it appears that your problem is that you're trying to use the initialisation list in the declaration in the header AND in the cpp file, you can't do that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by iMalc View Post
    When you use the constructor initialisation list that doesn't mean you can forego having a function body.
    You still need empty curly braces to give the constructor a body.
    I gave the body in the .cpp file, but the compiler doesn't seem to accept it.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by iMalc View Post
    When you use the constructor initialisation list that doesn't mean you can forego having a function body. You still need empty curly braces to give the constructor a body.
    That said, it appears that your problem is that you're trying to use the initialisation list in the declaration in the header AND in the cpp file, you can't do that.
    O..got it.
    That only has to be in the cpp file.
    No errors now.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by manasij7479 View Post
    O..got it.
    That only has to be in the cpp file.
    No errors now.
    You missed iMalc's point.

    The initialiser list is part of the constructor definition, as is the "body" of the constructor. The initialiser list and body of the constructor have to be together. That may be either in the header file or the separate source file. Not spread between the two. Not duplicated between the two.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by grumpy View Post
    You missed iMalc's point.

    The initialiser list is part of the constructor definition, as is the "body" of the constructor. The initialiser list and body of the constructor have to be together. That may be either in the header file or the separate source file. Not spread between the two. Not duplicated between the two.
    Yes.. I did exactly that.
    The header has:
    Keyword(std::string input);

    and the source has:
    Code:
    Keyword::Keyword(std::string input):token(input)
    {
        /*...*/
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-17-2008, 12:54 PM
  2. Replies: 8
    Last Post: 03-19-2008, 03:04 AM
  3. finding derived class type of a pointer to a base class
    By LinuxCoder in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2006, 11:08 AM
  4. Replies: 2
    Last Post: 04-06-2005, 07:25 AM
  5. Replies: 1
    Last Post: 12-11-2002, 10:31 PM