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
".
Related videos on Youtube
Comments
-
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:
- Why does it not Compile this way?
- 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 over 12 yearsYou should include all the relevant lines from the g++ error.
-
matth over 12 yearsThe 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 over 12 yearsI included the header file; I didn't provide enough information the first time I don't think.
-
Oliver Charlesworth over 9 years@narengi: because that's how the C++ standard defines the grammar.
-
dhein almost 9 yearsWhich is the "second" one? the one in the declarator or the on in its function definition?
-
maxdev over 8 years@Zaibis the second one is not the first one, but the second one.
-
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);
tovoid output(ostream &s, const Node *p);
would change anything of his described problem. -
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 almost 8 yearsThe 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 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 over 6 yearsI think this clarifies it stackoverflow.com/questions/558122/what-is-a-static-function/…
-
Hari over 2 yearsThere is a good explanation here: stackoverflow.com/a/31305772/1047213