
在类中声明并定义一个静态常量的问题。
//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");处出现错误,请问这里的错误原因是什么?
求大家帮忙解决,谢谢 展开
#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");处出现错误,请问这里的错误原因是什么?
求大家帮忙解决,谢谢 展开
展开全部
静态变量的赋值是在类外进行的
应该这样操作
static const int LIMIT;
类外初始化
const int Person::LIMIT = 25;
static const LIMIT=25;
这样写不对,没有声明LIMIT的变量类型
关于C++变量初始化 可以看看这个
http://hi.baidu.com/duckweek/blog/item/c20043d1d898bd0e3bf3cfe4.html
应该这样操作
static const int LIMIT;
类外初始化
const int Person::LIMIT = 25;
static const LIMIT=25;
这样写不对,没有声明LIMIT的变量类型
关于C++变量初始化 可以看看这个
http://hi.baidu.com/duckweek/blog/item/c20043d1d898bd0e3bf3cfe4.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询