Rob*_*obo 8 c++ parameters templates implicit forward-declaration
有一个带有隐式参数的模板类声明:
List.h
template <typename Item, const bool attribute = true>
class List: public OList <item, attribute>
{
public:
List() : OList<Item, attribute> () {}
....
};
Run Code Online (Sandbox Code Playgroud)
我尝试在不同的头文件中使用fllowing forward声明:
Analysis.h
template <typename T, const bool attribute = true>
class List;
Run Code Online (Sandbox Code Playgroud)
但是G ++显示了这个错误:
List.h:28: error: redefinition of default argument for `bool attribute'
Analysis.h:43: error: original definition appeared here
Run Code Online (Sandbox Code Playgroud)
如果我使用没有隐式参数的前向声明
template <typename T, const bool attribute>
class List;
Run Code Online (Sandbox Code Playgroud)
编译器不接受这种结构
Analysis.h
void function (List <Object> *list)
{
}
Run Code Online (Sandbox Code Playgroud)
并显示以下错误(即不接受隐含值):
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
Run Code Online (Sandbox Code Playgroud)
更新的问题:
我从模板定义中删除了默认参数:
List.h
template <typename Item, const bool attribute>
class List: public OList <item, attribute>
{
public:
List() : OList<Item, attribute> () {}
....
};
Run Code Online (Sandbox Code Playgroud)
使用类List的第一个文件具有带有参数属性隐式值的前向声明
Analysis1.h
template <typename T, const bool attribute = true>
class List; //OK
class Analysis1
{
void function(List <Object> *list); //OK
};
Run Code Online (Sandbox Code Playgroud)
第二个类使用类使用隐式值定义WITH前向定义
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
};
Run Code Online (Sandbox Code Playgroud)
第二个类使用隐式值使用类WITHOUT前向定义
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)
};
Run Code Online (Sandbox Code Playgroud)
简单.从定义中删除默认值,因为您已在前向声明中提到过该值.
template <typename Item, const bool attribute = true> //<--- remove this 'true`
class List: public OList <item, attribute>
{
//..
};
Run Code Online (Sandbox Code Playgroud)
写:
template <typename Item, const bool attribute> //<--- this is correct!
class List: public OList <item, attribute>
{
//..
};
Run Code Online (Sandbox Code Playgroud)
在线演示:http://www.ideone.com/oj0jK