【问题标题】:C++ Non-Integral template Const Initialization expected init-declarator before ClassNameC++ 非整数模板常量初始化预期在 ClassName 之前的 init-declarator
【发布时间】:2012-12-06 13:40:04
【问题描述】:

我正在尝试初始化一个非整数模板常量。

请在下面找到代码:

#ifndef _EXETENDED_CLASS_H
#define _EXETENDED_CLASS_H


template<class T>
class BaseClass
{
            public:
                   BaseClass();
                   ~BaseClass();


};

template <class T>
BaseClass<T>::BaseClass()
{}

template <class T>
BaseClass<T>::~BaseClass()
{}



template<class T>
class ExtendedClass:public BaseClass<T>
{
            public:
                   typedef ExtendedClass<T>* position;
                   static const position NULLPOSITION;

                   ExtendedClass();
                   ~ExtendedClass();


           private:

                   position _successivo; 
};


template<class T>
const  ExtendedClass<T>::position ExtendedClass<T>::NULLPOSITION = 0;

template <class T>
ExtendedClass<T>::ExtendedClass()
{}

template <class T>
ExtendedClass<T>::~ExtendedClass()
{}

#endif

问题在于线条

template<class T>
const  ExtendedClass<T>::position ExtendedClass<T>::NULLPOSITION = 0;

我无法初始化内联常量,因为它是非整数类型。

从我在网上阅读的内容看来,如果我将 const 初始化移动到 .cpp 文件中,问题就会消失。但是我不能这样做,因为我正在处理一个模板类。 我收到如下详细信息的错误:

ExtendedClass.h:43: error: expected init-declarator before "ExtendedClass"
ExtendedClass.h:43: error: expected `;' before "ExtendedClass"
make: *** [ExtendedClass.o] Error 1

有人可以帮我看看吗?非常感谢您的宝贵时间。

【问题讨论】:

    标签: c++ g++ initialization constants integral


    【解决方案1】:

    您已经编写了两次类型,但没有限定标识符。难怪可怜的编译器会糊涂。

    template<class T>
    const  ExtendedClass<T>::position ExtendedClass<T>::NULLPOSITION = 0;
    

    【讨论】:

    • 感谢您指出这一点。我错误地补充说,在尝试不同的 delation 变体试图自己修复它时。我没有根据你的建议编辑我的问题。但是我仍然得到编译器相同的错误。
    • 可能缺少typename then-typename ExtendedClass&lt;T&gt;::position
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多