Where to define derived class consructors ?

This is a discussion on Where to define derived class consructors ? within the C++ Programming forums, part of the General Programming Boards category; I thought they, as for general member functions can be defined either inside the class as an inline or in ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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 ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,194
    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%.

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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)
    {
        /*...*/
    }
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 09:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21