-2

i have created two classes A and B so i have 5 files , main.cpp , A.h , A.cpp , B.h , B.cpp

i have included all headers as it should be and im trying to create a object of class A in class B and i get the following error : error: A doesn't name a type

and if i to it repeated like i define object B in class A it works ,,, whats wrong?

this is how my B.h looks like >

#ifndef B_H
#define B_H

#include <iostream>
#include "A.h"

using namespace std;
class B
{
    public:
        B();
    protected:
    private:
        A instance;

};

#endif // B_H

and now A.h

#ifndef A_H
#define A_H

#include <iostream>
#include "B.h"

using namespace std;
class A
{
    public:
        A();
    protected:
    private:
};

#endif // A_H

2 Answers2

2

Your B.h includes A.h and A.h includes B.h. This leads you to something like B.h includes B.h.

From A.h delete the include "B.h". It is unused.

Simply Me
  • 1,579
  • 11
  • 23
  • @TheBestBoss, if any answer solved your problem, click the gray tick under the score to mark it as answer. – Simply Me Feb 14 '16 at 16:44
0

Let's look at A.h:

First it includes B.h. This pulls in the definition of class B, which uses class A. Next it defines class A. There's the problem. As others have stated removing the unused include will fix the problem.

Steve
  • 1,760
  • 10
  • 18