2011-02-05 87 views
7

有模板类的具有隐式参数的声明:模板与隐式参数,前向声明,C++

List.h

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

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

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 
}; 

使用类列表而不使用正向定义使用类列出与向前定义使用隐式值

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 
}; 

第二类第二类隐含值

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) 
}; 
+0

好吧,我明白你的问题。这是因为你在你使用* List的每个文件中添加前向声明。不要这样做。相反,在`List.h`中添加前向声明,您可以在每个使用`List`的文件中定义* List *和**`#include“List.h”`。让我知道你是否仍然面临问题! – Nawaz 2011-02-05 11:32:27

+0

那么更新后你的问题是什么? – UmmaGumma 2011-02-05 11:42:15

回答

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> 
{ 
    //.. 
}; 

在线演示:http://www.ideone.com/oj0jK

+1

谢谢,它的工作原理:-)。 – Robo 2011-02-05 10:01:02

0

您可以只在一个地方定义默认参数(对于给定的翻译)。在类声明中这样做是非常有用的,而在前面定义它是一个坏主意。

转发不需要默认参数(您只需在某些情况下键入它)。

如果你真的想要一个默认参数,你可以创建另一个简单的模板类型,它与forward一起实现。那么你通过typedef访问结果。你可以使用列表的前进来做到这一点。

0

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

0

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

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>() {} 
    ... 
}; 

而且Analysis.h

#include "List_fwd.h"