Can somebody help?
Suppose, I have the following C++ files:
#ifndef ROOM_H
#define ROOM_H
#include "Console.h"
...
class Room {
public:
void room();
};
#endif // ROOM_H
#include "Room.h"
void Room::room() {
Console::print();
}
#ifndef CONSOLE_H
#define CONSOLE_H
#include "Room.h"
#include "RoomChild.h"
class Console {
public:
static void print();
static void printRoom(Room& room);
static void printRoomChild(RoomChild& room);
};
#endif // CONSOLE_H
#include "Console.h"
void Console::print() {
;
}
void Console::printRoom(Room& room) {
// print member of room
}
void Console::printRoomChild(RoomChild& roomChild) {
// print member of roomChild
}
#ifndef ROOM_CHILD_H
#define ROOM_CHILD_H
#include "Room.h"
#include "Console.h"
class RoomChild : public Room {
public:
void roomChild();
};
#endif // ROOM_CHILD_H
#include "RoomChild.h"
void RoomChild::roomChild() {
Console::print();
}
I have tried to put forward declarations to each class or to some of them only. However, it does not work and give me either ... has incomplete type
, or expected class-name before '{' token class RoomChild: public Room {
The includes are all goofed up.
Delete the superfluous include directives from the files where they are irrelevant.
Add include directives on the files where they are depended upon.
Room.h
#ifndef ROOM_H
#define ROOM_H
/* DELETE: because Room.h does not depend on it.
#include "Console.h"
*/
// ...
class Room {
public:
void room();
};
#endif // ROOM_H
Room.cpp
#include "Room.h"
/* ADD: because Room.cpp does depend on it. */
#include "Console.h"
void Room::room() {
Console::print();
}
Console.h
#ifndef CONSOLE_H
#define CONSOLE_H
/* DELETE: because Console.h does not depend on them.
#include "Room.h"
#include "RoomChild.h"
*/
class Console {
public:
static void print();
};
#endif // CONSOLE_H
Console.cpp
#include "Console.h"
void Console::print() {
;
}
RoomChild.h
#ifndef ROOM_CHILD_H
#define ROOM_CHILD_H
#include "Room.h"
/* DELETE: because RoomChild.h does not depend on it
#include "Console.h"
*/
class RoomChild : public Room {
public:
void roomChild();
};
#endif // ROOM_CHILD_H
RoomChild.cpp
#include "RoomChild.h"
/* ADD: because RoomChild.cpp does depend on it. */
#include "Console.h"
void RoomChild::roomChild() {
Console::print();
}