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

1

169

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

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?

Signatures have always been the death of me when using 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);
Keeler answered 15/11, 2011 at 0:23 Comment(2)
You should include all the relevant lines from the g++ error.Beckham
The error message you list can't be produced by the code you posted. There is no Foo::Bar anywhere in your program fragment. Please post a complete, minimal program that demonstrates the error you are having. A complete program is one that we can compile exactly as-is and receive the same error message as you. A minimal program is one with every line unrelated to your error removed. The code fragment you posted is neither complete nor minimal. See sscce.org for more info.Pamphylia
T
422

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".

Turnout answered 15/11, 2011 at 0:26 Comment(10)
I included the header file; I didn't provide enough information the first time I don't think.Keeler
@narengi: because that's how the C++ standard defines the grammar.Turnout
Which is the "second" one? the one in the declarator or the on in its function definition?Brendin
@Zaibis the second one is not the first one, but the second one.Hypopituitarism
@maxdev: In that case I have to down vote the answer because I dont know how changing static void output(ostream &s, const Node *p); to void output(ostream &s, const Node *p); would change anything of his described problem.Brendin
@Zaibis, but it does: it tells to remove the double static definition of the function. You need to make the function static only once: at its declaration inside the classGlider
The keyword static does not have the same meaning in the method declaration than in the function definition. And a function (definition) cannot be static if it is a class' method (declaration). Hence, you can declare it static, but not define it static. In the function definition 'static' has the same meaning as in C, which is incompatible with a class method.Lodging
@dabicho: Finally an useful answer. Can you elaborate why in which way the function definition static is incompatible with a class function member?Creepy
I think this clarifies it https://mcmap.net/q/17187/-what-is-a-quot-static-quot-function-in-c/…Lodging
There is a good explanation here: https://mcmap.net/q/145158/-member-function-with-static-linkageGuienne

© 2022 - 2024 — McMap. All rights reserved.