具有隐式参数、前向声明、C++ 的模板
有一个带有隐式参数的模板类声明:
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
};
使用类 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)
};
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
template <typename T, const bool attribute = true>
class List;
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;
compiler does not accept this construction
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
Updated question:
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> () {}
....
};
The first file using class List has forward declaration with implicit value of the parameter attribute
Analysis1.h
template <typename T, const bool attribute = true>
class List; //OK
class Analysis1
{
void function(List <Object> *list); //OK
};
The second class using class List WITH forward definition using the implicit value
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
};
The second class using class List WITHOUT forward definition using the implicit value
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)
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简单的。从定义中删除默认值,因为您已经在前向声明中提到了这一点。
编写:
在线演示:http://www.ideone.com/oj0jK
Simple. Remove the default value from the definition, since you already mentioned that in the forward declaration.
Write:
Online Demo : http://www.ideone.com/oj0jK
一个可能的解决方案是声明另一个头文件 List_fwd.h,
因此在 List.h 和 Analysis.h 中,您都在开头包含 List_fwd.h。所以List.h变成
And Analysis.h
A possible solution is to declare an other header file, List_fwd.h
So in both List.h and Analysis.h you include List_fwd.h at the beginning. So List.h becomes
And Analysis.h
您必须确保只有第一个声明具有参数的默认值。这可以通过首先定义仅前向声明标头,然后将其包含在
List.h
和Analysis.h
中来实现。在List.h
的定义中,不要包含默认值。You must make sure only the first declaration has the default value of the parameter. This can be accomplished by first defining a forward-declaration-only header, then including it from both
List.h
andAnalysis.h
. In the definition inList.h
, do not include the default value.您只能在一处定义默认参数(对于给定的翻译)。在类声明中这样做是最有用的,在前面定义它是个坏主意。
转发不需要默认参数(在某些情况下您只需键入它)。
如果您确实想要一个默认参数,您可以创建另一个简单的模板类型,它与转发结合实现此功能。然后你可以通过 typedef 访问结果。您可以使用列表转发来做到这一点。
you may define the default parameter in only one place (for a given translation). it's most useful to do so at the class declaration, and a bad idea to define it at the forward.
a forward does not need the default parameter (you'll just have to type it in some cases).
you could create another simple template type which implements this in conjunction with a forward, if you really want a default parameter. then you access the result via a typedef. you can do this using a forward of List.
您必须将
List.h
包含在您使用它的每个文件中。声明仅适用于非模板类型。对于模板类型,您必须包含每个编译单元的头文件。You must include
List.h
in every file, where you are using it. Declaration is enought only for nontemplate types. For template types you must include header file for every compilation unit.