I am attempting to create a simple base class and another class that inherits from it. I am getting the following error.
guard_room.h:17:1: error: expected class-name before ‘{’ token
I have looked at other posts and it seems like I have a circular dependency somewhere but I have done everything I have read to resolve it. Here are my classes
Base class room.h:
#ifndef _room
#define _room
template <typename U, typename T>
class room
{
public:
//default constructor
room();
//overloaded constructor
room(U, T);
//getters/setters
void set_treasure(T);
void set_name(U);
T get_treasure() const;
U get_name() const;
private:
U name;
//room monster
//monster room_monster
T treasure;
};
#include "room.tem"
#endif
Inherited class guard_room.h:
#ifndef _guard_room
#define _guard_room
template <typename U, typename T> class room;
template <typename U, typename T>
class guard_room : public room
{
public:
//default constructor
guard_room();
//overloaded constructor
guard_room(U, T, T) : room(U, T);
//battle function?
void battle();
private:
T dummy;
};
#include "guard_room.tem"
#endif
I am also thoroughly confused in where I need to put the includes for the base room class in my inherited class. Thanks for your help with this.
Firstly, your derived class needs to be able to see the entire definition of your base class – you’ll need to include the header at the top of the derived class’ file. (a base class extends a derived class, so the base class is very much a part of the derived class)
Also, room is a template and not a class – you can only inherit from a concrete class, so you need to specify the template parameters. i.e.
Lastly – do your
.temfiles also contain header guards? (If not, you’ll need those in there too to avoid other compiler errors)Also, be aware that an initialiser list is part of a constructor’s definition, so the following is invalid
If your constructor definition is elsewhere, then all you want here is
otherwise, it might just be enough to write