C++ - Errors / Warnings - Cannot declare member function ‘static void MyClass::myMethod()’ to have static linkage [-fpermissive]

In C++, we don't need to specify the static keyword in the declaration of the header and in the definition in the class.

It means that this keyword has to be added only in the header.

You probably know that using the static keyword means that the function is unique in a file.
So if you add static in the .h and in the .cpp, you will have like two methods with the same name.
And the compiler won't appreciate it.

Let's take two examples, one not working and another yes.

We are going to implement 2 files:

  • MyClass.h
  • MyClass.cpp

The bad

MyClass.h

#ifndef MYCLASS_H_
#define MYCLASS_H_

class MyClass {
public:
    MyClass();
    virtual ~MyClass();

    static void myMethod();
};

#endif /* MYCLASS_H_ */

MyClass.cpp

#include "MyClass.h"

MyClass::MyClass() {
}

MyClass::~MyClass() {
}

static void MyClass::myMethod() {
}

The good

MyClass.h

#ifndef MYCLASS_H_
#define MYCLASS_H_

class MyClass {
public:
    MyClass();
    virtual ~MyClass();

    static void myMethod();
};

#endif /* MYCLASS_H_ */

MyClass.cpp

#include "MyClass.h"

MyClass::MyClass() {
}

MyClass::~MyClass() {
}

void MyClass::myMethod() {
}

Conclusion

A really annoying problem that you've just solved.
Great job. cool

Comments

Comment: 

Thanks!

I'm seeing that nobody said that, but may be this post has more visits than you think.

Comment: 

Thanks so much. I agree with Neimad. A very useful and well written post. Keep the good work going!

Comment: 

Thanks - short and to the point with no fluff.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.