带有字段的静态方法 [英] Static method with a field
本文介绍了带有字段的静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在将lua与C ++整合。对于lua我需要静态方法,我把它放在一个类。我需要静态方法与类中的一些字段进行通信(保存数据),但是当我尝试不同的方式它失败。它如下所示:
I am currently integrating lua with C++. For lua I need static methods which I've put in a class. I need the static method communicating (save data) with some fields in the class, but it fails when I try different ways. It goes like this:
class CClass{
private:
static int a;
public:
static int f();
}
,我尝试以这种方式实现f p>
and I try to implement the f() method in this manner:
int CClass::f() {
a = 5;
return 0;
}
但它给出了未解决的外部符号的错误。如何强制方法将数据保存在其中?
but it gives me error with unresolved external symbol. How to force the method to save my data in there?
谢谢。
推荐答案
大多数 static
成员需要在类外定义:
Most static
members need to be defined outside the class:
class CClass {
static int a;
// ...
};
int CClass::a; // in the .cpp file, not the header
这篇关于带有字段的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文