c++ 类模板示例化:基类未定义[重复]

ttygqcqt  于 2023-03-09  发布在  其他
关注(0)|答案(2)|浏览(109)

此问题在此处已有答案

Base class undefined(4个答案)
昨天关门了。
给定以下类模板

template <typename T, typename VT, typename PF, typename ... Sub>
class InheritableAccess;

template <typename T, typename VT, typename PF, typename F, typename ... Sub>
class InheritableAccess<T, VT, PF, F, Sub...> : public InheritableAccess<T, typename F::Type, F, Sub...>{};

template <typename T, typename PF, typename ... Sub>
class InheritableAccess<T, T, PF, Sub...>{};

正在尝试示例化它

internal::InheritableAccess<inheritable, sub1::Type, sub1, sub2> t;

产生以下编译错误:

Test.h(32,1): error C2504: 'internal::InheritableAccess<T,SubItem2,F>': base class undefined
1>        with
1>        [
1>            T=inheritable,
1>            F=sub2
1>        ]
1>test-project2.cpp(21): message : see reference to class template instantiation 'internal::InheritableAccess<inheritable,SubItem1,sub1,sub2>' being compiled

为什么会这样?
可运行代码示例如下:
Test.h

#pragma once

template <typename T, typename A, int8_t I = 0>
struct SubClass
{
    typedef T Type;
    typedef A AttributeType;
    static const int8_t id = I;
};
namespace internal {
    template <typename T, typename VT, typename PF, typename ... Sub>
    class InheritableAccess;

    template <typename T, typename VT, typename PF, typename F, typename ... Sub>
    class InheritableAccess<T, VT, PF, F, Sub...> : public InheritableAccess<T, typename F::Type, F, Sub...>
    {

    };

    template <typename T, typename PF, typename ... Sub>
    class InheritableAccess<T, T, PF, Sub...>
    {
    };
}

template <typename Super, typename ... Sub>
class Inheritable;

Test.cpp

#include <iostream>
#include "Test.h"

class Item {
};

class SubItem1 : Item {};
class SubItem2 : Item {};
class SubItem3 : Item {};

class ItemAttr {};

int main()
{
    using sub1 = SubClass<SubItem1, ItemAttr, 1>;
    using sub2 = SubClass<SubItem2, ItemAttr, 2>;
    using sub3 = SubClass<SubItem3, ItemAttr, 3>;
    using inheritable = Inheritable<Item, sub1, sub2, sub3>;
    internal::InheritableAccess<inheritable, sub1::Type, sub1, sub2> t;
    return 0;
}
0lvr5msh

0lvr5msh1#

internal::InheritableAccess模板没有定义。
增加:

namespace internal {
    template <typename T, typename VT, typename PF, typename ... Sub>
    class InheritableAccess{};
//                         ^^
lymgl2op

lymgl2op2#

在使用派生的东西(比如从类中派生类)之前,你必须完全完成类定义的BODY。
您的基类

template <typename T, typename VT, typename PF, typename ... Sub>
class InheritableAccess;

还没有身体

相关问题