Thread: Inheritance stops inheriting between step two and three

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    Inheritance stops inheriting between step two and three

    I am becoming silly about this problem.

    I can't seem to be able to get an inheritance past the second step of inheritance.

    I need Person->Student->Vip inheritance, but somehow it stops responding to inheritance between Student and Vip.

    the error is:No matching function STUD::STUD(); in the Vip::Vip() constructor
    main.cpp
    Code:
    int main() {
    	Person empl("BILLY", "HOME");
    	STUD student("BOBBY", "OUTSIDE", 18);
    	Vip VIPPER("Lisa", "OFFICE", 45);
    
    	std::cout << "Navn: " << empl.HentNavn() << std::endl;
    	std::cout << "Adresse: " << empl.HentAdresse() << std::endl;
        std::cout << "Navn: " << student.HentNavn() << std::endl;
        std::cout << "Alder: " << student.HentAlder() << std::endl;
        std::cout << "Navn: " << VIPPER.HentNavn() << std::endl;
        std::cout << "Alder: " << VIPPER.HentAlder() << std::endl;
        std::cout << "Løn: " << VIPPER.HentLoen() << std::endl;
    
        return 0;
    }
    functions.cpp:
    Code:
    #include <iostream>
    #include "person.h"
    #include "stud.h"
    #include "vip.h"
    
    Person::Person(std::string Navn, std::string Adresse)
    {
    	itsNavn = Navn;
    	itsAdresse = Adresse;
    }
    std::string Person::HentNavn() const { return itsNavn; }
    std::string Person::HentAdresse() const { return itsAdresse; }
    
    Person::~Person()
    {
    	std::cout << "destructor Person called... \n";
    }
    
    STUD::STUD(
    	std::string Navn, std::string Adresse, int Alder):
    	Person(Navn, Adresse), itsAlder(Alder)
    {
    	std::cout << "STUD constructor...\n";
    }
    
    int STUD::HentAlder() const { return itsAlder; }
    
    STUD::~STUD()
    {
    	std::cout << "destructor STUD called...\n";
    }
    
    //the error is:No matching function STUD::STUD();
    Vip::Vip(std::string Navn, std::string Adresse, int Loen):
    	Person(Navn, Adresse)
    {
    	itsLoen = Loen;
    	std::cout << "Vip constructor...\n";
    }
    
    int Vip::HentLoen() const {return itsLoen; }
    
    Vip::~Vip()
    {
    	std::cout << "destructor Vip called...\n";
    }
    person.h:
    Code:
    #ifndef PERSON_H_
    #define PERSON_H_
    
    
    class Person {
    public:
    	Person(std::string Navn, std::string Adresse);
    
    	std::string virtual HentNavn() const;
    	std::string virtual HentAdresse() const;
    
    	~Person();
    protected:
    	std::string itsNavn;
    	std::string itsAdresse;
    };
    
    
    #endif /* PERSON_H_ */
    stud.h
    Code:
    #ifndef STUD_H_
    #define STUD_H_
    
    #include "person.h"
    
    class STUD : public virtual Person {
    public:
    	STUD(std::string Navn, std::string Adresse, int Alder);
    
    	int virtual HentAlder() const;
    
    	~STUD();
    protected:
    	int itsAlder;
    };
    
    
    #endif /* STUD_H_ */
    vip.h
    Code:
    #ifndef VIP_H_
    #define VIP_H_
    
    #include "stud.h"
    
    class Vip : public STUD {
    public:
    	Vip(std::string Navn, std::string Adresse, int Loen);
    
    	int HentLoen() const;
    
    	~Vip();
    protected:
    	int itsLoen;
    };
    
    
    #endif /* VIP_H_ */

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by Roffemuffe View Post
    the error is:[I]No matching function STUD::STUD();
    That is because there is no STUD::STUD() defined. You override generation of the default constructor by defining STUD(std::string Navn, std::string Adresse, int Alder). You must therefore declare the default constructor explicitly, which should solve your problem.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    I'm not sure I understand...

    I did try to make a STUD(); in the class definition. No difference.

    I was under the impression the Vip::Vip( /*... */); -constructor was causing the problems?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When you try to construct an object that has derived from one or more classes, like Vip and Stud, the compiler will first call the derived class's default constructor. In this case, that would be Stud::Stud(). But no such constructor exists, hence you get a compile error.
    There are two ways to solve this.
    Either you create a default constructor in Stud, or
    you use the initializer list in Vip to call an appropriate Stud constructor as you've done for Person.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheriting iterators
    By dudeomanodude in forum C++ Programming
    Replies: 15
    Last Post: 03-10-2008, 01:42 PM
  2. inherited variables in template class not inheriting
    By fimion in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2005, 08:10 AM
  3. Inheriting static attributes
    By Slink99 in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2005, 02:08 PM
  4. Inheriting from specific templated version of class
    By skiingwiz in forum C++ Programming
    Replies: 4
    Last Post: 11-14-2004, 09:21 AM
  5. This never stops for some reason
    By Shadow12345 in forum C++ Programming
    Replies: 14
    Last Post: 09-02-2002, 10:41 AM