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

Comment: 

thx for clarifying that

Comment: 

You have saved my life!

Comment: 

Thank you very much.

Comment: 

Appreciate the help

Comment: 

Thank you so much stranger.

Comment: 

I am so thankful for this post.

Comment: 

Thank you. It's simple but it's need to know.

Comment: 

Thank you very much for the clear and concise explanation.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.