一个类的定义,关于CONST的

来源:百度知道 编辑:UC知道 时间:2024/05/22 20:12:52
这是我定义的一个类
#ifndef _ACCOUNT_H
#define _ACCOUNT_H

class Account{
private:
static const int MAXSIZE = 100;
char name[20];
char date[MAXSIZE][20];
int money[MAXSIZE];
int cnt; //记录操作数
int sum;
static int total;
public:
Account(char*);
~Account();
void accountIn(char*, int);
void accountOut(char*, int);
char* getname() const;
void disp();
static void total();
friend void compare(Account&, Account&);
};

int Account::total=0;

#endif
编译器报错:
static const int MAXSIZE = 100;
这一行是error C2258: illegal pure syntax, must be '= 0'
error C2252: 'MAXSIZE' : pure specifier can only be specified for functions

对于这一行char date[MAXSIZE][20];
error C2057: expected constant expression
warning C4200: nonstandard extension used : zero-sized array in struct/union

static const int MAXSIZE = 100;
这句是错.的.不能在类里边赋static数据成员的初值
必须在类外赋.
在类外这样写:int Account::MAXSIZE=100;
char date[MAXSIZE][20]; 错是因为你的MAXSIZE在用之前没定义.
总的说,程序组织有错误;
可以像下面这样来;
static const int MAXSIZE=100;
class Account{
private:

char name[20];
char date[MAXSIZE][20];
int money[MAXSIZE];
int cnt; //记录操作数
int sum;
static int total;
public:
Account(char*);
~Account();
void accountIn(char*, int);
void accountOut(char*, int);
char* getname() const;
void disp();
static void total(); //这个total()函数与数据成员同名了.要改掉.
friend void compare(Account&, Account&);
};

int Account::total=0;

两种方法:
1>
class Account{
private:
static const int MAXSIZE;
//...
};
然后
//必须在类外面赋值
static const int Account::MAXSIZE = 100;

2>用枚举enum,这种方法比较常用
class Account{

private:
enum Size