c++ opp, Fehlermeldung bei Vererbung



  • Hallo zusammen,

    bei meinen ersten Gehversuchen mit c++ bin ich nun leider auf ein kleines Problem gestroßen.

    Ich habe 4 Dateien mit 2 Klassen und der main Funktion.

    Will ich das Programm nun ausführen kommt folgende Fehlermeldung 😡

    7 G:\PortableApps\Dev-CppPortable\Projekte\test\man.h:1, from main.cpp In file included from man.h:1, from main.cpp
    7 G:\PortableApps\Dev-CppPortable\Projekte\test\main.cpp from main.cpp
    1 G:\PortableApps\Dev-CppPortable\Projekte\test\human.h redefinition of class Human' 1 G:\\PortableApps\\Dev-CppPortable\\Projekte\\test\\human.h previous definition ofclass Human'
    G:\PortableApps\Dev-CppPortable\Projekte\test\Makefile.win [Build Error] [main.o] Error 1

    Leider verstehe ich das das Problem nicht. Vielleicht habe ich die Schreibweise
    nicht richtig verstanden.

    Könnt ihr mir da vielleicht helfen?

    main.cpp

    #include <cstdlib>
    #include <iostream>
    #include <string> 
    
    #include "human.h" //klasse human wird geladen
    #include "man.h" //klasse man wird geladen
    
    using namespace std;
    
    int main(void)
    {
    
        Man stephan();
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    

    human.h

    class Human {
    
       public: //variablen
          char name;
          int  age;
          int  height;	
          int  weight;
    
       public: //methoden
               Human(void); //Konstruktormethode
               Human(int age, int height, int weight); //Konstruktormethode
    
               ~Human(); //Destruktor
    
          int  GetAge(void) const;
          void SetAge(int age);
    
          int  GetHeight(void) const;
          void SetHeight(int height);
    
          int  GetWeight(void) const;
          void SetWeight(int weight);
    
    };
    

    human.cpp

    #include "human.h"
    
    Human::Human(int age, int height, int weight) { //Konstruktor methode
        this->SetAge(age);
        this->SetHeight(height);
        this->SetWeight(weight);
    }
    
    Human::~Human() {
    }
    
    int Human::GetAge(void) const {
        return this->age;
    }
    void Human::SetAge(int age) {
       this->age = age;
    }
    
    int Human::GetHeight(void) const {
       return this->height;
    }
    void Human::SetHeight(int height) {
       this->height = height;
    }
    
    int Human::GetWeight(void) const {
       return this->weight;
    }
    void Human::SetWeight(int weight) {
       this->weight = weight;
    }
    

    man.h

    #include "human.h"
    
    class Man : public Human {
    
          public:
               Man(void); //Konstruktormethode
               ~Man(); //Destruktormethode
    
    };
    

    man.cpp

    #include "man.h"
    
    Man::Man() {
    }
    Man::~Man() {
    }
    


  • Deine Header brauchen Include Guards.



  • btw:

    int main(void) -> int main()

    Man stephan(); -> Man stephan;

    return EXIT_SUCCESS; -> kannst du auch weglassen

    public: //variablen
          char name;
          int  age;
          int  height;	
          int  weight;
    

    1. wieso public?
    2. char name macht bestimmt was ganz anderes, als du willst : D
    versuchs mal mit std::string name und ganz oben noch #include <string>

    Human(void) -> Human() - hast du übrigens vergessen zu implementieren

    ~Human(); brauchst du nicht - aber wenn du Polymorphie willst, solltest du überlegen, dort virtual ~Human() {} zu schreiben

    Human::Human(int age, int height, int weight) { //Konstruktor methode
        this->SetAge(age);
        this->SetHeight(height);
        this->SetWeight(weight);
    }
    

    1.) Initialisierungsliste
    2.) wieso jedes mal this-> ?

    #include "human.h"
    
    class Man : public Human {
    
          public:
               Man(void); //Konstruktormethode
               ~Man(); //Destruktormethode
    
    };
    
    Man::Man() {
    }
    Man::~Man() {
    }
    

    wenn du das so lassen möchtest, dann reicht es auch so:

    #include "human.h"
    
    class Man : public Human {};
    

    bb


Anmelden zum Antworten