Thread: constructors and inheritance

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    constructors and inheritance

    anyone have any idea what this error message could mean?
    I have a class Person that inherits from another class Name.
    Person class does not have a constructor. Name has a constructor.
    Im not sure if im doing it correclty..
    Name constructor takes a few parameters like so..
    Code:
    Name(char * firstname, char * middlename, char * surname)
    Wheni create a new person object i want to do something like
    Code:
    Person jack("jack","ant","smith");
    error: base 'Person' with only non-default constructor in class
    without a constructor
    ?

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    if i have
    Name.h
    Code:
    class Name {
    enum State { Teen, Child, Adult, Senior };
    Name(char * firstname, char * surname, Name::State status);
    ~Name(void);
    virtual void getAddress(void);
    Person.h
    Code:
    #include "Name.h"
    class Person: public Name {
    Person(char *first, char *surname,Name::State status) : Name(first,surname,status) {}
    main.cpp
    Code:
    #include "Person.h"
    Person p("jack", "smith",State::Child);
    why do i get hte following errors?
    Code:
    /tmp/ccwoDlOf.o(.gnu.linkonce.t._ZN9PersonC1EPKcS1_N7State4statusE+0x1f): In function `Person::Person[in-charge](char const*, char const*, State::status)':
    : undefined reference to `vtable for Person'
    collect2: ld returned 1 exit status
    make: *** [main] Error 1
    ??

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    19
    I haven't made any tests. But as i can see your constructor of name is private. That means nobody can "directly" construct an instance of this class. Is this really what you want ?

    mfg JJ

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    hrm well after i've put
    Code:
    class Name {
    public:
    enum State { Teen, Child, Adult, Senior };
    Name(char * firstname, char * surname, Name::State status);
    ~Name();
    still same stuff happens

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    In your main.cpp you can't do

    Person p("jack", "smith",State::Child);

    Because State is not a known type. You need to use Name::State::Child

    Also you need to fill out your name destructor and virtual function.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    Also you need to fill out your name destructor and virtual function.
    what do you mean?

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    if i do
    Name jac("jack", "smith",State::Child);
    this works
    so how come me doing
    Person p("jack", "smith",State::Child);
    wont work ?

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    This compiles okay for me.
    Code:
     
    #include <iostream>
    using namespace std;
    class Name 
    {
    public:
      enum State { Teen, Child, Adult, Senior };
      Name(){};
      Name(char * firstname, char * surname, State status);
      
      virtual void getAddress(void){};
      char fn[80];
      char sn[80];
      State status_;
    };
    Name::Name(char * firstname, char * surname, State status)
    {
      strcpy(fn, firstname);
      strcpy(sn, surname);
      status_ = status;
    }
    
    class Person: public Name 
    {
    public:
      Person(char *first, char *surname, State status) : Name(first,surname,status) 
      {};
      void getAddress(void){};
    };
    int main()
    {
      Person p("jack", "smith", Name::Child);
      return 0;
    }
    The reason for the changes were:
    1) as a nested type within Name, the compiler can look within Name for the declaration of the type State and figure out what

    State status

    means without using the scope resolution operator. However,

    2) in main() the compiler can't tell where or what State is because it's buried in Name. Therefore, if I tell the compiler to look in Name for a variable called Child by using the scope resolution operator, it can resolve the type of Child to the appropriate type called State, and everybody's happy. And:

    3) I added a getAddress() to Person so it had it's own version of the virtual function in Name.

    Or, at least I think that's why it compiled okay.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    yes your code works,
    but when i follow the same structure, and place it into my seperate files
    Name.cpp Name.h Person.h Person.cpp Main.cpp
    it all dies..
    and i get the same error message mentioned above

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    ok could someone check it?

    this is PERSON.h
    Code:
    #ifndef _PERSON_H_
    #define _PERSON_H_
    #include "Name.h"
    using namespace std;
    class Person: public Name 
    {
    public:
      Person(char *first, char *surname, State status) : Name(first,surname,status) 
      {};
      void getAddress(void){};
    };
    #endif
    this is PERSON.CPP
    Code:
    #include "Person.h"
    #include "Name.h"
    using namespace std;
    void Person::getAddress(void) {
    //nothing yet
    }
    this is NAME.h
    Code:
    #ifndef _NAME_H_
    #define _NAME_H_
    #include <iostream>
    using namespace std;
    class Name 
    {
    public:
      enum State { Teen, Child, Adult, Senior };
      Name(){};
      Name(char * firstname, char * surname, State status);
      
      virtual void getAddress(void){};
    protected:
      char fn[80];
      char sn[80];
      State status_;
    };
    #endif
    This is NAME.cpp
    Code:
    #include "Name.h"
    using namespace std;
    Name::Name(char * firstname, char * surname, State status)
    {
      strcpy(fn, firstname);
      strcpy(sn, surname);
      status_ = status;
    }
    this is MAIN.cpp
    Code:
    #include "Name.h"
    #include "Person.h"
    int main()
    {
      Person p("jack", "smith", Name::Child);
      return 0;
    }

    and hte error is
    Code:
    /tmp/ccwoDlOf.o(.gnu.linkonce.t._ZN9PersonC1EPKcS1_N7State4statusE+0x1f): In function `Person::Person[in-charge](char const*, char const*, State::status)':
    : undefined reference to `vtable for Person'
    collect2: ld returned 1 exit status
    make: *** [main] Error 1

  11. #11
    meow nbk's Avatar
    Join Date
    Jul 2004
    Posts
    45
    First off, the getAddress is declared inline and in the .h file...

    *Had to leave, sorry for no big description*

    (now that I think of it, the inline, I don't think, would be a problem)
    Last edited by nbk; 08-15-2004 at 10:34 AM.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    This organizational pattern and code compiles without error in MSVC 6.0. It doesn't do anything. But that's beside the point.

    Code:
    //Name.h
    #ifndef NAME_H
    #define NAME_H
    class Name 
    {
    public:
    	enum State { Teen, Child, Adult, Senior };
    	Name(){};
    	Name(char * firstname, char * surname, State status);
     
    	virtual void getAddress(void){};
     
    	char fn[80];
    	char sn[80];
    	State status_;
    };
    #endif
    Code:
    //Name.cpp
    #include "Name.h"
    #include <cstring>
    Name::Name(char * firstname, char * surname, State status)
    {
      strcpy(fn, firstname);
      strcpy(sn, surname);
      status_ = status;
    }
    Code:
    //Pearson.h
    #ifndef PEARSON_H
    #define PEARSON_H
    #include "Name.h"
    class Person: public Name 
    {
    	public:
    	 Person(char *first, char *surname, State status) : Name(first,surname,status) 
    		{};
    	 void getAddress(void){};
    };
    #endif
     
    //there is no Pearson.cpp since all constructors methods in Pearson.h are inlined given their simplicity
    Code:
    //driver.cpp
    #include <iostream>
    #include "Pearson.h"
    using namespace std;
    int main()
    {
      Person p("jack", "smith", Name::Child);
      return 0;
    }
    Last edited by elad; 08-15-2004 at 12:55 PM.

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheritance and constructors
    By coletek in forum C++ Programming
    Replies: 6
    Last Post: 07-04-2009, 01:35 AM
  2. How constructors are called in c++ (inheritance)
    By Spirytus in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2007, 06:18 PM
  3. Inheritance!
    By kidburla in forum C++ Programming
    Replies: 7
    Last Post: 11-14-2005, 11:44 AM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM