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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8130066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:03:37  来源:igfitidea点击:

Static member functions error; How to properly write the signature?

c++static-membersmethod-signaturestatic-functions

提问by Joshua

I am getting an error when trying to compile my code in g++ using the current signature:

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

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

My question is twofold:

我的问题是双重的:

  1. Why does it not Compile this way?
  2. What is the correct signature, and why?
  1. 为什么不这样编译?
  2. 什么是正确的签名,为什么?

Signatures have always been the death of me when using C++

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

Edit: Here is the class header file, as well:

编辑:这里也是类头文件:

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);

回答by Oliver Charlesworth

I'm guessing you've done something like:

我猜你已经做了类似的事情:

class Foo
{
    static void Bar();
};

...

static void Foo::Bar()
{
    ...
}

The "static void Foo::Bar" is incorrect. You don't need the second "static".

static void Foo::Bar”不正确。你不需要第二个“ static”。