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

90,925

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

Share:
90,925

Related videos on Youtube

Joshua
Author by

Joshua

PHP/LaTeX

Updated on July 08, 2022

Comments

  • Joshua
    Joshua almost 2 years

    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);
    
    • Keith Layne
      Keith Layne over 12 years
      You should include all the relevant lines from the g++ error.
    • matth
      matth over 12 years
      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.
  • Joshua
    Joshua over 12 years
    I included the header file; I didn't provide enough information the first time I don't think.
  • Oliver Charlesworth
    Oliver Charlesworth over 9 years
    @narengi: because that's how the C++ standard defines the grammar.
  • dhein
    dhein almost 9 years
    Which is the "second" one? the one in the declarator or the on in its function definition?
  • maxdev
    maxdev over 8 years
    @Zaibis the second one is not the first one, but the second one.
  • dhein
    dhein over 8 years
    @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.
  • Alex
    Alex over 8 years
    @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
  • dabicho
    dabicho almost 8 years
    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.
  • Multisync
    Multisync over 6 years
    @dabicho: Finally an useful answer. Can you elaborate why in which way the function definition static is incompatible with a class function member?
  • dabicho
    dabicho over 6 years
  • Hari
    Hari over 2 years
    There is a good explanation here: stackoverflow.com/a/31305772/1047213