gpt4 book ai didi

c++ - 尽管类名与 C++ 完全匹配,但模板化基类初始化构造函数失败

转载 作者:太空宇宙 更新时间:2023-11-04 16:30:09 25 4
gpt4 key购买 nike

在下面的代码中,我在初始化列表中有一个项目

ExtendHelper<Extender>::Type<>

与基类完全匹配:

ExtendHelper<Extender>::Type<>

但是 VS2010 编译器报错:error C2614: 'Base::Type<T>::Extend<Extender>' : illegal member initialization: 'Type<Base::Type<Base::Type<Base>::ExtendHelper<Data> >::ExtendHelper<Data> >' is not a base or member .

这怎么可能?

struct Base
{
template <class T=Base>
struct Type
{
template <class Extender>
struct ExtendHelper
{
template <class U=ExtendHelper>
struct Type
:
T::Type<U>,
Extender
{
Type(typename T::Type<T> &rhs)
:
T::Type<U>(rhs)
{
}
Type() {}
};
};

template <class Extender>
struct Extend
:
ExtendHelper<Extender>::Type<>
{
template <class C>
Extend(C &rhs)
:
ExtendHelper<Extender>::Type<>(rhs)
{
}
};
};
};

struct Data { };

Base::Type<Base> base;
Base::Type<Base>::Extend<Data> baseWithData(base);

编辑:请注意,我必须向 ExtendHelper 添加默认构造函数因为编译器想要一个,但我不知道为什么。

最佳答案

您需要指定嵌套名称Type指的是模板。从字面上替换所有出现的 ::Type<通过 ::template Type< .

(这不是唯一的问题;还有一个与 ExtendHelper 的构造函数有关的问题,我仍在努力弄清楚。)

更新:我认为你的外层 Type类缺少构造函数。如果添加以下两个,它将起作用:

template <typename U> Type(const U &) { }  // needed for `Base::Type<Base>&` in the constructor initializer
Type() { } // needed for `base` in the example code

关于c++ - 尽管类名与 C++ 完全匹配,但模板化基类初始化构造函数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7968830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com