静态成员函数错误; 如何正确写签名?

Jos*_*hua 161 c++ static-members method-signature static-functions

尝试使用当前签名在g ++中编译我的代码时出错:

cannot declare member function static void Foo::Bar(std::ostream&, const Foo::Node*) to have static linkage
Run Code Online (Sandbox Code Playgroud)

我的问题是双重的:

  1. 为什么不这样编译?
  2. 什么是正确的签名,为什么?

在使用C++时,签名一直是我的死

编辑:这是类头文件,以及:

class Foo {


public:
    Foo();

    ~Foo();

    bool insert(const Foo2 &v);

    Foo * find(const Foo2 &v);

    const Foo * find(const Foo2 &v) const;

    void output(ostream &s) const;

private:
    //Foo(const Foo &v);
    //Foo& operator =(const Foo &v);
    //Not implemented; unneeded


    struct Node {
        Foo2 info;
        Node *left;
        Node *right;
    };

    Node * root;

    static bool insert(const Foo2 &v, Node *&p);


    static void output(ostream &s, const Node *p);


    static void deleteAll(Node *p);
Run Code Online (Sandbox Code Playgroud)

Oli*_*rth 411

我猜你做过类似的事情:

class Foo
{
    static void Bar();
};

...

static void Foo::Bar()
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

" static void Foo::Bar"不正确.你不需要第二个" static".

  • @Zaibis第二个不是第一个,而是第二个. (43认同)
  • @Oliver:但为什么呢? (25认同)
  • 关键字static在方法声明中的含义与在函数定义中的含义不同.如果函数(定义)是类的方法(声明),则它不能是静态的.因此,您可以将其声明为静态,但不能将其定义为静态.在函数定义中,"static"具有与C中相同的含义,这与类方法不兼容. (24认同)
  • @narengi:因为这就是C++标准定义语法的方式. (14认同)
  • 哪个是"第二个"?声明器中的那个或函数定义中的on? (2认同)
  • @Zaibis,但确实如此:它告诉删除函数的双重静态定义。您只需将函数设为静态一次:在“类”内的声明中 (2认同)