模板隐式参数、前向声明、C++

8
有一个带有隐式参数的模板类声明:

List.h

template <typename Item, const bool attribute = true>
class List: public OList <item, attribute>
{
    public:
    List() : OList<Item, attribute> () {}
    ....
};

我试图在不同的头文件中使用以下前向声明:

Analysis.h

template <typename T, const bool attribute = true>
class List;

但是G++显示了这个错误:
List.h:28: error: redefinition of default argument for `bool attribute'
Analysis.h:43: error:   original definition appeared here

如果我使用不带隐式参数的前向声明
template <typename T, const bool attribute>
class List;

编译器不接受这种结构。

Analysis.h

void function (List <Object> *list)
{
}

并显示以下错误(即不接受隐式值):
Analysis.h:55: error: wrong number of template arguments (1, should be 2)
Analysis.h:44: error: provided for `template<class T, bool destructable> struct List'
Analysis.h:55: error: ISO C++ forbids declaration of `list' with no type

更新的问题:

我从模板定义中删除了默认参数:

List.h

template <typename Item, const bool attribute>
class List: public OList <item, attribute>
{
    public:
    List() : OList<Item, attribute> () {}
    ....
};

使用List类的第一个文件具有参数属性的隐式值的前向声明。

Analysis1.h

template <typename T, const bool attribute = true>
class List;  //OK

class Analysis1
{
    void function(List <Object> *list); //OK
};

使用隐式值的前向定义的 class List 的第二个类

Analysis2.h

template <typename T, const bool attribute = true> // Redefinition of default argument for `bool attribute'
class List; 

class Analysis2
{
    void function(List <Object> *list); //OK
};

使用隐式值的不带前向定义的 List 类的第二个类

Analysis2.h

template <typename T, const bool attribute> // OK
class List; 

class Analysis2
{
    void function(List <Object> *list); //Wrong number of template arguments (1, should be 2)
};

好的,我理解了你的问题。这是因为你在每个使用List的文件中都添加了前向声明。不要这样做。相反,在定义ListList.h中添加前向声明,并在每个使用List的文件中包含#include "List.h"。如果你仍然遇到问题,请告诉我! - Nawaz
更新后你有什么问题吗? - UmmaGumma
5个回答

5

简单。从定义中删除默认值,因为您已经在前向声明中提到了它。

template <typename Item, const bool attribute = true> //<--- remove this 'true`
class List: public OList <item, attribute>
{
  //..
};

写:

template <typename Item, const bool attribute>  //<--- this is correct!
class List: public OList <item, attribute>
{
  //..
};

Online Demo : http://www.ideone.com/oj0jK


@Robo:如果它起作用了,那么请点击勾号"接受"此答案。到目前为止,您甚至没有接受一个答案! - Nawaz
@Nawaz:我更喜欢在声明中设置默认值,而不是在前向声明中。我认为这样更清晰明了。 - jopasserat
@Robo: 很令人失望和沮丧的是,你不接受答案。你连一个答案都没有接受。当你不接受答案时,你觉得为什么会有人愿意回答你的问题呢? - Nawaz
我接受了你的答案,感谢你的时间... 但是这个模型对于具有模板默认参数的多个类不起作用... - Robo
@Robo:好的,我明白你的问题了。这是因为你在每个使用List的文件中都添加了前向声明。不要这样做。相反,在定义ListList.h中添加前向声明,并在每个使用List的文件中包含#include "List.h"。如果你仍然遇到问题,请告诉我! - Nawaz
显示剩余2条评论

2
一种可能的解决方案是声明另一个头文件 List_fwd.h。
template <typename Item, const bool attribute>
class List;

所以在List.h和Analysis.h中,你都需要在开头包含List_fwd.h。因此,List.h变成了:
#include "List_fwd.h"

template <typename Item, const bool attribute = true>
class List: public OList <item, attribute>
{
    public:
    List() : OList<Item, attribute> () {}
    ...
};

分析.h

#include "List_fwd.h"

1

您必须确保只有第一个声明具有参数的默认值。这可以通过首先定义仅前向声明的头文件,然后从List.hAnalysis.h中包含它来实现。在List.h的定义中,不要包含默认值。


0

你可以在一个地方(对于给定的翻译)定义默认参数。最好在类声明时这样做,而在前向声明中定义它是一个坏主意。

前向声明不需要默认参数(在某些情况下,您只需键入它即可)。

如果您真的想要一个默认参数,那么您可以创建另一个简单的模板类型,并与前向声明一起实现它。然后,您可以通过typedef访问结果。您可以使用List的前向声明来完成此操作。


0

在使用它的每个文件中,您必须包含List.h。仅对于非模板类型,声明就足够了。对于模板类型,您必须为每个编译单元包含头文件。


但是如何使用这个模型避免头文件中的循环依赖呢? - Robo
@Robo 我不是说你不能声明它,我是说你必须包含头文件。为了解决依赖关系,你可能必须在包含头文件之前声明类。但你仍然需要包含头文件。 - UmmaGumma
@Robo 顺便说一下,在包含之前不需要声明它 :) - UmmaGumma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接