在类中声明并定义一个静态常量的问题。

//person.cpp#include<iostream>#include<string>usingnamespacestd;classPerson{private:s... //person.cpp
#include<iostream>
#include<string>
using namespace std;
class Person
{
private:
static const LIMIT=25;请问怎么在类里声明并定义一个静态常量(在不用enum的情况下)。
string lname;
char fname[LIMIT];
public:
Person(){lname="",fname[0]='\0'}
Person(const string &ln,const char* fn="Heyyou");
void show()const;
void Formalshow()const;
};
Person::Person(const string &ln,const char* fn="Heyyou")
{
lname=ln;
strcpy(fname,fn);
}
void Person::show()const
{
cout<<fname<<" "<<lname<<endl;
}
void Person::Formalshow()const
{
cout<<lname<<","<<fname<<endl;
}

//主程序
#include<iostream>
#include"Person.cpp"
int main()
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy","Sam");
one.show();
cout<<endl;
one.Formalshow();
two.show();
cout<<endl;
two.Formalshow();
three.show();
cout<<endl;
three.Formalshow();
return 0;
}
static const LIMIT=25;处出现错误。请问怎么在类里声明并定义一个静态常量(在不用enum的情况下),我的编译器是vc6。
Person(const string &ln,const char* fn="Heyyou");处出现错误,请问这里的错误原因是什么?
求大家帮忙解决,谢谢
展开
 我来答
duck_week
2011-05-21 · TA获得超过148个赞
知道小有建树答主
回答量:106
采纳率:0%
帮助的人:149万
展开全部
静态变量的赋值是在类外进行的
应该这样操作

static const int LIMIT;

类外初始化

const int Person::LIMIT = 25;

static const LIMIT=25;
这样写不对,没有声明LIMIT的变量类型
关于C++变量初始化 可以看看这个
http://hi.baidu.com/duckweek/blog/item/c20043d1d898bd0e3bf3cfe4.html
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式