错误C2270:非成员函数不允许使用修饰符

moo*_*isy 2 c++ compiler-errors modifiers non-member-functions visual-studio-2012

编译时我收到此错误:

错误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;
    }
Run Code Online (Sandbox Code Playgroud)

谢谢!

qua*_*dev 7

你有一个错误C2270,因为你的busco函数是一个免费的模板函数,它不属于一个类.因此const签名没有任何意义:删除它.

如果您希望此函数是成员函数,请将其定义放在声明点(我猜这个ABBImp类),或者使用类名称为Recuperar函数添加前缀.

  • +1 - 注意它*在类中声明*,所以你的第二段可能是OP想要的. (2认同)