编译时出现这个错误:
错误 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;
}
谢谢!
最佳答案
您遇到错误 C2270,因为您的 busco
函数是一个自由模板函数,它不属于某个类。所以 const
对签名没有意义:删除它。
如果您希望此函数成为一个成员函数,请将其定义放在声明点(我猜是 ABBImp
类),或者像您对Recuperar
函数。
关于c++ - 错误 C2270 : Modifiers not allowed on nonmember functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23976021/