Jonas
Jonas

Reputation: 391

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?

Upvotes: 29

Views: 106659

Answers (5)

garrettmills
garrettmills

Reputation: 822

Not related to OP's issue, but for any other C++ learners stumbling upon this, I was getting this error for a different reason. If your parent class is templated, you need to specify the type name in the child class:

#include "Parent.h"

template <typename ChildType>
class Child : public Parent<ChildType> { // <ChildType> is important here

};

Upvotes: 17

chmoder
chmoder

Reputation: 976

Check that #ifndef and #define in your header file is unique.

#ifndef BASE_CLIENT_HANDLER_H
#define BASE_CLIENT_HANDLER_H

#include "Threads/Thread.h"

class BaseClientHandler : public threads::Thread {
public:
    bool isOn();
};
#endif //BASE_CLIENT_HANDLER_H

Upvotes: 6

yuklai
yuklai

Reputation: 1655

In daughter.cpp, switch the two lines of include. i.e.

#include "mother.h"
#include "daughter.h"

What happened was that the compiler is looking into the definition of class daughter and could not find the definition of the base class mother. So it's telling you that "I'm expecting the identifier mother in front of "{" in the line

class daughter: public mother {

to be a class, but I can't find it's definition!"

In mother.cpp, remove the inclusion of daughter.h. The compiler does not need to know the definition of daughter.h; i.e. class mother can be used without daughter. Adding the inclusion of daughter.h introduces unnecessary dependency between the class definitions.

On the other hand, it is always better IMHO to keep the inclusion of header in the definition of the class (.cpp) and not the declaration of the class (.h). This way it is less likely you need to resolve header inclusion nightmare when including headers that in turn include other headers which you don't have control. But many production code includes header in header. Both are correct, just need to be careful when you do that.

Upvotes: 12

Luchian Grigore
Luchian Grigore

Reputation: 258548

First off, you have include guards in implementation files. Remove them.

Second off, if you inherit from a class, you need to include the header where the class is defined.

Upvotes: 5

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361254

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.

Upvotes: 33

Related Questions