在线
客服

发布
需求

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

新手求助,c++静态函数调用的问题

#includeusing namespace std;class A{private:static int x;public:A(int a){x=a;}static void print();};void A::print(){cout

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

全部参与3

C++类的静态成员变量是需要定义的,即 分配内存。
如果没有分配空间,就无法访问到静态成员。
调试方法:
intA::a;//添加在类的定义部分,使staticinta有定义//和voidA::print()函数的定义,是同样的道理

使用道具 举报

静态成员需在类外部初始化
#includeusingnamespacestd;classA{private:staticinta;public:A(intx){a=x;}staticvoidprint();};voidA::print(){cout

使用道具 举报

static成员函数只能访问static成员变量。x并不是static的,所以报错。

使用道具 举报

发新帖
国内首家创新型IT技术需求众包服务平台,软件需求就上天盟网! 立即登录 立即注册