C++ C++;-未定义类型的使用无效…-“类别”声明

C++ C++;-未定义类型的使用无效…-“类别”声明,c++,class,inheritance,declaration,C++,Class,Inheritance,Declaration,我有超类和子类,其中子类继承自超类 在超类中,我有一个常量属性,它的值取决于使用它的子类。然而,我需要在超类中声明它,因为超类中还有其他一些方法也使用它,但是我需要在子类中初始化它,因为常量的值根据实例化的子类类型而变化 从一个例子中,我知道最好的解决方案是使用trait类。然而,使用这样的解决方案需要对我的代码进行大量更改。因此,我选择了这里显示的方法 超类.h #ifndef SUPERCLASS_H #define SUPERCLASS_H #include <string>

我有超类和子类,其中子类继承自超类

在超类中,我有一个常量属性,它的值取决于使用它的子类。然而,我需要在超类中声明它,因为超类中还有其他一些方法也使用它,但是我需要在子类中初始化它,因为常量的值根据实例化的子类类型而变化

从一个例子中,我知道最好的解决方案是使用trait类。然而,使用这样的解决方案需要对我的代码进行大量更改。因此,我选择了这里显示的方法

超类.h

#ifndef SUPERCLASS_H
#define SUPERCLASS_H


#include <string>

template <class T, class P>
class SuperClass
{
      public:

       typedef T type;
       typedef P position;

       static const position NULLPOSITION;

};

#endif
#ifndef SUBCLASS_H
#define SUBCLASS_H

#include <string>
#include "SuperClass.h"


template <class T>
class SubClass:public SuperClass<T,int>
{

};

template<class T>
const typename SuperClass<T,int>::position SuperClass<T,int>::NULLPOSITION=0;

#endif
\ifndef超类
#定义超类
#包括
模板
类超类
{
公众:
T型;
类型定义P位置;
静态常数位置零位;
};
#恩迪夫
子类.h

#ifndef SUPERCLASS_H
#define SUPERCLASS_H


#include <string>

template <class T, class P>
class SuperClass
{
      public:

       typedef T type;
       typedef P position;

       static const position NULLPOSITION;

};

#endif
#ifndef SUBCLASS_H
#define SUBCLASS_H

#include <string>
#include "SuperClass.h"


template <class T>
class SubClass:public SuperClass<T,int>
{

};

template<class T>
const typename SuperClass<T,int>::position SuperClass<T,int>::NULLPOSITION=0;

#endif
#ifndef子类
#定义子类
#包括
#包括“超类.h”
模板
类子类:公共超类
{
};
模板
const typename超类::position超类::NULLPOSITION=0;
#恩迪夫
main.cpp

#include <cstdlib>
#include <iostream>
#include "SubClass.h"

using namespace std;

int main(int argc, char *argv[])
{
    SubClass<int> subClass;

    system("PAUSE");
    return EXIT_SUCCESS;
}
#包括
#包括
#包括“h子类”
使用名称空间std;
int main(int argc,char*argv[])
{
亚类;
系统(“暂停”);
返回退出成功;
}
在编译时我得到

invalid use of undefined type `class SuperClass<T, int>
未定义类型`class超类的使用无效

类超类的声明
错误。
问题可能是什么?

通过专门化您的方式,您实际上并没有实例化NULLPOSITION(或POSIZIONENULLA,请检查您的代码)

14.7.1.2

特别是,初始化(以及任何相关的副作用) 静态数据成员不会出现,除非该静态数据成员是 它本身的使用方式需要定义静态数据 存在的成员

您可能希望使用另一个类明确定义数据成员,如中所示

template<typename P>
class PositionClass
{
    public:
        typedef P position;
        static const position NULLPOSITION;
};
template <typename T, class P>
class SuperClass : public PositionClass<P>
{
    public:
        typedef T type;
};

const PositionClass<int>::position  PositionClass<int>::NULLPOSITION = 0;
模板
类位置类
{
公众:
类型定义P位置;
静态常数位置零位;
};
模板
类超类:公共位置类

{ 公众: T型; }; 常量PositionClass::position PositionClass::NULLPOSITION=0;


问题在于您对
空位置的定义。您已经为模板
超类
声明了静态成员
NULLPOSITION
,但尚未定义它。相反,您尝试为其部分显式实例化定义成员。您应该删除部分显式实例化定义,并为
NULLPOSITION
定义常规模板类静态成员定义

要允许子类为
NULLPOSITION
提供不同的初始化值,可以通过(可能是可选的)模板参数来实现

template <class T, class P, P INIT>
class SuperClass
{
public:
    typedef T type;
    typedef P position;
    static const position NULLPOSITION;
};

template<class T, class P, P INIT>
const typename SuperClass<T,P,INIT>::position
    SuperClass<T,P,INIT>::NULLPOSITION = INIT;

template <class T>
class SubClass:public SuperClass<T,int, 0>
{
};
模板
类超类
{
公众:
T型;
类型定义P位置;
静态常数位置零位;
};
模板
常量typename超类::位置
超类::NULLPOSITION=INIT;
模板
类子类:公共超类
{
};

请不要叫你的类。@Borgleader(最好叫它
类类
结构类
类结构
结构
,对吗?:P)@Borgleader好的,我相应地编辑了这个问题。
模板const typename SuperClass::position SuperClass::NULLPOSITION=0在超类定义下。