I'm trying to learn c++ and I've stumbled upon a error while trying to figuring out inheritance.
Compiling: daughter.cpp In file included from /home/jonas/kodning/testing/daughter.cpp:1: /home/jonas/kodning/testing/daughter.h:6: error: expected class-name before ‘{’ token Process terminated with status 1 (0 minutes, 0 seconds) 1 errors, 0 warnings
My files: main.cpp:
#include "mother.h" #include "daughter.h" #include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; mother mom; mom.saywhat(); return 0; } mother.cpp:
#include "mother.h" #include "daughter.h" #include <iostream> using namespace std; mother::mother() { //ctor } void mother::saywhat() { cout << "WHAAAAAAT" << endl; } mother.h:
#ifndef MOTHER_H #define MOTHER_H class mother { public: mother(); void saywhat(); protected: private: }; #endif // MOTHER_H daughter.h:
#ifndef DAUGHTER_H #define DAUGHTER_H class daughter: public mother { public: daughter(); protected: private: }; #endif // DAUGHTER_H and daughter.cpp:
#include "daughter.h" #include "mother.h" #include <iostream> using namespace std; daughter::daughter() { //ctor } What I want to do is to let daughter inherit everything public from the mother class (=saywhat()). What am I doing wrong?
You forgot to include mother.h here:
#ifndef DAUGHTER_H #define DAUGHTER_H #include "mother.h" //<--- this line is added by me. class daughter: public mother { public: daughter(); protected: private: }; #endif // DAUGHTER_H You need to include this header, because daughter is derived from mother. So the compiler needs to know the definition of mother.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With