以下用 g++ 编译得很好:
struct acounter {
long static counter;
void static create() { //reset or create the counter
counter=0;
}
void static count() { //the counter changes its internal value.
counter=counter + 1;
}
};
int main(int argc, char *argv[] ){ //compiles and executes!
//do some random stuff...
return 0;
}
问题是:只要我添加“acounter::create();” 或“acounter::count();” 到主循环,我得到一个错误:
未定义对“acounter::counter”的引用
但我定义了“计数器”,甚至初始化了它。问题是什么?
(PS 我只能使用静态函数,因为我必须稍后处理回调 - 想法是仅在其全局范围内使用整个结构而不创建实例。)