【问题标题】:Error C2270: Modifiers not allowed on nonmember functions错误 C2270:非成员函数上不允许使用修饰符
【发布时间】:2014-06-01 01:37:01
【问题描述】:

编译时出现此错误:

错误 C2270: 'busco' : 非成员函数上不允许修饰符

我想我明白原因,但我不知道如何解决它,如果我把 const 拿出来,我会收到 C2662 错误。

代码如下:

    template <class T>
    class ABBImp: public ABB<T> {
    public:
        const T& Recuperar(const T &e) const ;
    private:
        NodoABB<T> * busco(NodoABB<T> * arbol,T &e) const;
    protected:
        NodoABB<T>* arbol;
    };

    template <class T>
//If I take this const out I get the other error I talked about
    NodoABB<T>* busco(NodoABB<T> * arbol,T &e)const{
        if(a!=NULL){
            if(arbol->dato==e)
                return arbol;
            else if (arbol->dato<e)
                return busco(arbol->der,e);
            else
                return busco(arbol->izq,e);
        }else{
            return NULL;
        }
    }
    template <class T>
    const T& ABBImp<T>::Recuperar(const T &e) const{
        NodoABB<T> * aux=busco(arbol,e);
        return aux->dato;
    }

谢谢!

【问题讨论】:

    标签: c++ visual-studio-2012 compiler-errors modifiers non-member-functions


    【解决方案1】:

    你有一个错误 C2270,因为你的 busco 函数是一个免费的模板函数,它不属于一个类。所以const 对签名毫无意义:删除它。

    如果您希望此函数成为成员函数,请将其定义放在声明点(我猜是 ABBImp 类),或者像 Recuperar 函数那样在声明前加上类名。

    【讨论】:

    • +1 -- 请注意它在类中声明的,所以你的第二段可能是OP想要的。
    • 谢谢!我没有意识到我忘记了这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2012-08-02
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    相关资源
    最近更新 更多