static成员变量问题 4行代码 却报错 郁闷~

walle_huan 2008-10-28 11:43:57
#include<iostream.h>
class Cest
{
public:
static int ss;
};

void main()
{
int Cest::ss=0;
}


报:\Documents and Settings\sisi\桌面\test\mytest.cpp(10) : error C2655: 'ss' : definition or redeclaration illegal in current scope
C:\Documents and Settings\sisi\桌面\test\mytest.cpp(5) : see declaration of 'ss'

我看书上一直都是这样定义的,想自己试试,可就是通不过,谁来帮帮我这个新手
...全文
107 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lbzhao_28 2008-10-29
  • 打赏
  • 举报
回复
放在语句块外就好了.
liumingrong 2008-10-28
  • 打赏
  • 举报
回复
这样写不是静态变量的初始化,编译器会认为是重复定义
将int Cest::ss=0; 放到语句块之外
[Quote=引用楼主 bloodshadow 的帖子:]
#include <iostream.h>
class Cest
{
public:
static int ss;
};

void main()
{
int Cest::ss=0;
}


报:\Documents and Settings\sisi\桌面\test\mytest.cpp(10) : error C2655: 'ss' : definition or redeclaration illegal in current scope
C:\Documents and Settings\sisi\桌面\test\mytest.cpp(5) : see declaration of 'ss'

我看书上一直都是这样定义的,想自己试试,可就是通不过…
[/Quote]
maplele20 2008-10-28
  • 打赏
  • 举报
回复

class Cest
{
public:
static int ss;
};
int Cest::ss = 0;
void main()
{
}
walle_huan 2008-10-28
  • 打赏
  • 举报
回复
哦 原来是这样 谢了 结贴 送分
Lovegarfield 2008-10-28
  • 打赏
  • 举报
回复
不能在main中定义
killbug2004 2008-10-28
  • 打赏
  • 举报
回复
#include <iostream> 
using namespace std;
class Cest
{
public:
static int ss;
};
int Cest::ss = 0;
void main()
{
Cest::ss = 88;
cout<<Cest::ss<<endl;
cin.get();
}
Fighting Horse 2008-10-28
  • 打赏
  • 举报
回复
int Cest::ss=0; 要定义在类相同的名字空间内,不能跑到main里面了
killbug2004 2008-10-28
  • 打赏
  • 举报
回复
类的静态成员变量不能在main中初始化
walle_huan 2008-10-28
  • 打赏
  • 举报
回复
静态变量初始化是不需要建对象的。你可以拿这串代码放到你机子上试试 我怀疑是不是我的VC++有问题!
tian428 2008-10-28
  • 打赏
  • 举报
回复
还没定义对象啊?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧