I get the error:
file.cpp:20: undefined reference to `MyClass::arr'
At this line, I have:
#include "MyClass.hpp"
extern "C" {
void MyClass::func() {
开发者_高级运维arr = 0;
}
At header:
class MyClass {
public:
static int arr;
static void func();
}
P.S. gcc (4.x) is called with: -Xlinker -zmuldefs
to avoid multiple definition checking.
This makes no sense :
#include <MyClass.hpp>
extern "C" {
void MyClass::func() {
arr = 0;
}
write
#include <MyClass.hpp>
int MyClass::arr = 0; // needs to be instantiated to satisfy linker.
void MyClass::func()
{
arr = 0;
}
implementation
#include "MyClass.hpp"
void MyClass::func()
{
this->arr = 0;
}
header file
class MyClass
{
public:
static int arr;
static void func();
}
Static class fields, after being declared in the class
statement, must be also defined in a single .cpp
file. In such file you should put:
int MyClass::arr;
By the way, the #include
statements have <>
brackets only when you're including system headers; for your own headers you should use the usual double quotes (""
).
精彩评论