有模板类的具有隐式参数的声明:模板与隐式参数,前向声明,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)
};
好吧,我明白你的问题。这是因为你在你使用* List的每个文件中添加前向声明。不要这样做。相反,在`List.h`中添加前向声明,您可以在每个使用`List`的文件中定义* List *和**`#include“List.h”`。让我知道你是否仍然面临问题! – Nawaz 2011-02-05 11:32:27
那么更新后你的问题是什么? – UmmaGumma 2011-02-05 11:42:15