c++ - 错误 C2270 : Modifiers not allowed on nonmember functions

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

编译时出现这个错误:

错误 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/

相关文章:

c++ - 带占位符的 FormatMessage

c++ - 在 C++ 中获取 DATETIME 戳的最简单方法

c++ - C/C++ : source file after preprocessing-Diab compiler?

macros - 在 Visual Studio 2012 (C#) 中如何创建 Post Build 中可用的自定义宏

visual-studio-2012 - TF400324 - 源代码管理资源管理器中的 "Page not found"

c++11 - _mm_free 作为 unique_ptr 的删除器

c++ - Hook 静态链接的 ELF 二进制文件

.net - 将托管 C++ 添加到 C# GUI

c++ - 链接静态类成员函数会引发 undefined reference 错误C++

struct - 在以下上下文中如何解决 "lifetime of reference outlives lifetime of borrowed content"?