错误:带有模板的'{''标记前的期望类名 [英] error: expected class-name before ‘{’ token with templates

查看:243
本文介绍了错误:带有模板的'{''标记前的期望类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的基类和从它继承的另一个类。我收到以下错误。



guard_room.h:17:1:error:'{'token



我已经看过其他帖子,似乎我有一个循环依赖的地方,但我已经做了我读过的一切,以解决它。这是我的课程

 

#ifndef _room
#define _room

template< typename U,typename T>
class room
{
public:

//默认构造函数
room();

//重载构造函数
room(U,T);


// getters / setters
void set_treasure(T);
void set_name(U);
T get_treasure()const;
U get_name()const;

private:

U name;

//房间怪物
// monster room_monster

T宝藏;

};

#includeroom.tem

#endif


$ b b

继承类guard_room.h:

  #ifndef _guard_room 
#define _guard_room

template< typename U,typename T>教室

template< typename U,typename T>
class guard_room:public room
{
public:

//默认构造函数
guard_room();

//重载构造函数
guard_room(U,T,T):room(U,T);

//战斗功能?
void battle();

private:
T dummy;

};

#includeguard_room.tem
#endif

我也完全困惑在哪里我需要把基地房间类的包含在我继承的类。感谢您的帮助。

解决方案

首先,您的派生类需要能够看到基类的整个定义 - 你需要在头文件的顶部包含头文件。 (一个基类扩展一个派生类,所以基类是派生类的一部分)



房间是一个模板,而不是一个类 - 你只能继承一个具体的类,所以你需要指定模板参数。

  #includeroom.h

template< typename U,typename T&
class guard_room:public room< U,T>

最后 - 您的 .tem 档包含标题保护? (如果没有,你也需要那些在那里,以避免其他编译器错误)






初始化列表是构造函数定义的一部分,所以以下是无效的:

  guard_room(U,T,T) ,T); 

如果你的构造函数定义在别处,那么你想要的是

  guard_room(U,T,T); 

否则,可能只需写入

  guard_room(U u,T t1,T t2):room(u,t1){} 

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.

#include "room.h"

template <typename U, typename T>
class guard_room : public room<U, T>

Lastly - do your .tem files 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

    guard_room(U, T, T) : room(U, T);

If your constructor definition is elsewhere, then all you want here is

    guard_room(U, T, T);

otherwise, it might just be enough to write

    guard_room(U u, T t1, T t2) : room(u, t1) {}

这篇关于错误:带有模板的'{''标记前的期望类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆