-2

I'm pretty new to C++ and working with books and youtube and here in the same time, but i got a weird problem that I can't find a solution. I'm trying to inherit from a base class "Mother" to a derived class Daughter and it is not working for me, it says it need to

This code was taking from TheNewBoston, so thank u Bucky, just giving credits

C:\CppProjects\inheritance\Daughter.h|6|error: expected class-name before '{' token

I'm adding the main and the classes:

main:

#include <iostream>
#include "Mother.h"
#include "Daughter.h"
using namespace std;

int main()
{
    Daughter t;
    t.sayName();
}

Mother class:

#include "Mother.h"
#include <iostream>
#include "Daughter.h"
using namespace std;
Mother::Mother()
{
}
void Mother::sayName(){
    cout<<"I am !!!"<<endl;
}

Mother header:

#ifndef MOTHER_H
#define MOTHER_H
class Mother
{
    public:
        Mother();
        void sayName();
};
#endif // MOTHER_H

Daughter class:

#include "Daughter.h"
#include <iostream>
#include "Mother.h"
using namespace std;
Daughter::Daughter()
{
}

Daughter header:

#ifndef DAUGHTER_H
#define DAUGHTER_H
class Daughter : public Mother
{
    public:
        Daughter();
};
#endif // DAUGHTER_H

I looked in most of the searches in here and nothing found, I even tried noob style and added std::, it has nothing to do with it.

thank you all of you who are willing to try!

just so you know, when I'm trying to put all of the code in one file, it works great:

the new main:

#include <iostream>
using namespace std;

class Mother
{
public:
    Mother();
    void sayName(){
        cout<<"I am!!!!"<<endl;
    }
};

class Daughter: public Mother
{
public:
    Daughter();
};

int main()
{
    Daughter t;
    t.sayName();
}
shimon
  • 86
  • 7
  • 4
    At least when including the daughter header it knows nothing of a class called Mother – Sami Kuhmonen Apr 16 '16 at 06:29
  • could you please explain further? and a way to fix it? – shimon Apr 16 '16 at 06:31
  • 3
    Think of what the definitions do. When the compiler starts on your Daughter.cpp, what does it see? It sees a link to Daughter.h. there is a class saying "I'm inherited from Mother", but you haven't told the compiler what a Mother is. So you must first have an include for a definition of Mother. Order is important. – Sami Kuhmonen Apr 16 '16 at 06:33
  • great! thank u it worked!!, add it as an answer, I'm even noober than I thought – shimon Apr 16 '16 at 06:35
  • [using namespace std is bad](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) - Please read – Ed Heal Apr 16 '16 at 06:45

1 Answers1

2

The order of the declarations is important. At the moment in your Daughter class you include a header for Daughter and only afterwards Mother. This means that the compiler will see a definition "hi, I'm Daughter, I inherited Mother" and will stop there wondering "I've never met your Mother, I don't know what to do here."

The order of includes and definitions is important. Always have the base classes first.

Usually you either include the base header in the daughter header, or at least have a forward declaration to tell that the Mother class does exist. Also check up on header guards since you will need them.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74