-1

I have myclassA and myClassB. I want to pass a ClassA object into ClassB's constructor. therefore I include A's header into B and B's header into A, but I get this error

'myclassB' does not name a type

Here is the code that I simplified:

myclassa.h

#ifndef XMLHANDLER_H
#define XMLHANDLER_H
#include "mainwindow.h"
#include "myclassb.h"

class myclassA{
public:
    myclassA();

private:
    myclassB *mb;
};
#endif // XMLHANDLER_H

myclassb.h

#ifndef CLASSB_H
#define CLASSB_H
#include "myclassa.h"

class myclassB{
public:
    myclassB(myclassA *newclass);
    ~myclassB();
};
#endif // CLASSB_H

myclassa.cpp

#include "myclassa.h"

myclassA::myclassA(){}

myclassb.cpp

#include "myclassb.h"

myclassB::myclassB(myclassA *newclass)
{
    //do something
}

How can I fix the error?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
sven
  • 1,101
  • 7
  • 21
  • 44

2 Answers2

2

There is no need to include header myclassB in header myclassA

Or you can create a separate header that will contain declarations

class MyClassA; class MyClassB;

and then include this header in other two.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

You need to declare class myclassB; in myclassa.h.

Eutherpy
  • 4,471
  • 7
  • 40
  • 64