具有隐式参数的模板,前向声明,C ++ [英] Templates with implicit parameters, forward declaration, C++
问题描述
有一个带有隐式参数的模板类声明:
There is a declaration of template class with implicit parameters:
List.h
template <typename Item, const bool attribute = true>
class List: public OList <item, attribute>
{
public:
List() : OList<Item, attribute> () {}
....
};
我试图在另一个头文件中使用顺延式声明:
I tried to use the fllowing forward declaration in a different header file:
Analysis.h
Analysis.h
template <typename T, const bool attribute = true>
class List;
但是G ++显示此错误:
But G++ shows this error:
List.h:28: error: redefinition of default argument for `bool attribute'
Analysis.h:43: error: original definition appeared here
如果我使用不带隐式参数的前向声明
If I use the forward declaration without implicit parameters
template <typename T, const bool attribute>
class List;
编译器不接受此构造
Analysis.h
Analysis.h
void function (List <Object> *list)
{
}
并显示以下错误(即不接受隐式值):
and shows the following error (i.e. does not accept the implicit value):
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
更新的问题:
我从模板定义中删除了默认参数:
I removed the default parameter from the template definition:
List.h
template <typename Item, const bool attribute>
class List: public OList <item, attribute>
{
public:
List() : OList<Item, attribute> () {}
....
};
使用类List的第一个文件具有带有参数属性隐式值的前向声明
The first file using class List has forward declaration with implicit value of the parameter attribute
Analysis1.h
Analysis1.h
template <typename T, const bool attribute = true>
class List; //OK
class Analysis1
{
void function(List <Object> *list); //OK
};
第二个使用List WITH类的类,使用隐式值进行正向定义
The second class using class List WITH forward definition using the implicit value
Analysis2.h
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的类,而没有使用隐式值进行正向定义
The second class using class List WITHOUT forward definition using the implicit value
Analysis2.h
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)
};
推荐答案
简单.从定义中删除默认值,因为您已经在前向声明中提到了该值.
Simple. Remove the default value from the definition, since you already mentioned that in the forward declaration.
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
这篇关于具有隐式参数的模板,前向声明,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!