简体   繁体   English

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

[英]Templates with implicit parameters, forward declaration, C++

There is a declaration of template class with implicit parameters: 有一个带有隐式参数的模板类声明:

List.h 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: 我尝试在不同的头文件中使用fllowing forward声明:

Analysis.h Analysis.h

template <typename T, const bool attribute = true>
class List;

But G++ shows this error: 但是G ++显示了这个错误:

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 Analysis.h

void function (List <Object> *list)
{
}

and shows the following error (ie 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 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 使用类List的第一个文件具有带有参数属性隐式值的前向声明

Analysis1.h 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 第二个类使用类使用隐式值定义WITH前向定义

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

The second class using class List WITHOUT forward definition using the implicit value 第二个类使用隐式值使用类WITHOUT前向定义

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

Write: 写:

template <typename Item, const bool attribute>  //<--- this is correct!
class List: public OList <item, attribute>
{
  //..
};

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

A possible solution is to declare an other header file, List_fwd.h 一种可能的解决方案是声明另一个头文件List_fwd.h

template <typename Item, const bool attribute>
class List;

So in both List.h and Analysis.h you include List_fwd.h at the beginning. 所以在List.h和Analysis.h中都包含List_fwd.h。 So List.h becomes 所以List.h成了

#include "List_fwd.h"

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

And Analysis.h 和Analysis.h

#include "List_fwd.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 and Analysis.h . 这可以通过首先定义一个仅向前声明的头,然后从List.hAnalysis.h包含它来List.h In the definition in List.h , do not include the default value. List.h的定义中,不要包含默认值。

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. 如果你真的想要一个默认参数,你可以创建另一个简单的模板类型,它与forward一起实现它。 then you access the result via a typedef. 然后通过typedef访问结果。 you can do this using a forward of List. 你可以使用前进列表来做到这一点。

You must include List.h in every file, where you are using it. 您必须在每个使用它的文件中包含List.h Declaration is enought only for nontemplate types. 声明仅适用于非模板类型。 For template types you must include header file for every compilation unit. 对于模板类型,必须包含每个编译单元的头文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM