c++类中怎么使用static成员变量,请详细解答

2025-05-19 03:19:27
推荐回答(2个)
回答1:

类的静态变量要放在类外初始化。

#include 
#define maxn 1000010
using namespace std;
class test
{
public:
    static int a;
    test(int x):b(x){}
    void print()
    {
        cout<    }
private:
    int b;
};
int test::a=12; // 在类外初始化

int main()
{
   test c(23);
   cout<   c.print();
    return 0;
}

回答2:

你试试换成pravite,我不确定【我不觉得哪里有错(ΦωΦ)】,一般成员变量都设成pravite