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

发布于 2024-10-15 21:45:35 字数 2146 浏览 4 评论 0原文

有一个带有隐式参数的模板类声明:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

赴月观长安 2024-10-22 21:45:35

简单的。从定义中删除默认值,因为您已经在前向声明中提到了这一点。

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

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

゛时过境迁 2024-10-22 21:45:35

一个可能的解决方案是声明另一个头文件 List_fwd.h,

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

因此在 List.h 和 Analysis.h 中,您都在开头包含 List_fwd.h。所以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

#include "List_fwd.h"

A possible solution is to declare an other header file, 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. So List.h becomes

#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

#include "List_fwd.h"
恋竹姑娘 2024-10-22 21:45:35

您必须确保只有第一个声明具有参数的默认值。这可以通过首先定义仅前向声明标头,然后将其包含在 List.hAnalysis.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 and Analysis.h. In the definition in List.h, do not include the default value.

鯉魚旗 2024-10-22 21:45:35

您只能在一处定义默认参数(对于给定的翻译)。在类声明中这样做是最有用的,在前面定义它是个坏主意。

转发不需要默认参数(在某些情况下您只需键入它)。

如果您确实想要一个默认参数,您可以创建另一个简单的模板类型,它与转发结合实现此功能。然后你可以通过 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.

楠木可依 2024-10-22 21:45:35

您必须将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文