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);
2
  • You should include all the relevant lines from the g++ error. Nov 15, 2011 at 0: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 sscce.org for more info.
    – Robᵩ
    Nov 15, 2011 at 1:08

1 Answer 1

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

10
  • 15
    @narengi: because that's how the C++ standard defines the grammar. Jan 27, 2015 at 8:28
  • 2
    Which is the "second" one? the one in the declarator or the on in its function definition?
    – dhein
    Aug 14, 2015 at 13:19
  • 45
    @Zaibis the second one is not the first one, but the second one.
    – maxdev
    Oct 9, 2015 at 14:35
  • 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, 2015 at 2: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 7, 2016 at 17:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.