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);
Joshua
  • 4,270
  • 10
  • 42
  • 62
  • You should include *all* the relevant lines from the g++ error. – Keith Layne Nov 15 '11 at 00:49
  • 5
    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 http://sscce.org for more info. – Robᵩ Nov 15 '11 at 01:08

1 Answers1

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

jjxtra
  • 20,415
  • 16
  • 100
  • 140
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • I included the header file; I didn't provide enough information the first time I don't think. – Joshua Nov 15 '11 at 00:33
  • 15
    @narengi: because that's how the C++ standard defines the grammar. – Oliver Charlesworth Jan 27 '15 at 08:28
  • 2
    Which is the "second" one? the one in the declarator or the on in its function definition? – dhein Aug 14 '15 at 13:19
  • 45
    @Zaibis the second one is not the first one, but the second one. – maxdev Oct 09 '15 at 14:35
  • @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. – dhein Oct 12 '15 at 09:17
  • 2
    @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 `class` – Alex Oct 15 '15 at 02:35
  • 26
    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. – dabicho Sep 07 '16 at 17:32
  • @dabicho: Finally an useful answer. Can you elaborate why in which way the function definition `static` is incompatible with a class function member? – Multisync Oct 24 '17 at 12:44
  • I think this clarifies it https://stackoverflow.com/questions/558122/what-is-a-static-function/558201#558201 – dabicho Oct 24 '17 at 14:27
  • There is a good explanation here: https://stackoverflow.com/a/31305772/1047213 – Hari Nov 04 '21 at 05:50