[英]C++: gcc can't find static member when linking
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();
}
PS gcc (4.x) is called with: -Xlinker -zmuldefs
to avoid multiple definition checking. 使用以下
-Xlinker -zmuldefs
调用PS gcc(4.x):- -Xlinker -zmuldefs
以避免进行多个定义检查。
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. 静态类领域,在被宣布之后
class
的语句,也必须在一个规定.cpp
文件。 In such file you should put: 在这样的文件中,您应该输入:
int MyClass::arr;
By the way, the #include
statements have <>
brackets only when you're including system headers; 顺便说一下,仅当您包含系统头文件时,
#include
语句才用<>
括起来; for your own headers you should use the usual double quotes ( ""
). 对于您自己的标题,应使用常用的双引号(
""
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.