Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very basic inheritance: error: expected class-name before ‘{’ token

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?

like image 861
Jonas Avatar asked Jul 02 '12 16:07

Jonas


1 Answers

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.

like image 111
Nawaz Avatar answered Sep 28 '22 03:09

Nawaz