一个C++错误,不知什么原因
来源:百度知道 编辑:UC知道 时间:2024/06/01 17:54:51
如下:
#include <iostream>
using namespace std;
class employee
{
public:
employee(char* name_str)
{
next_id++;
id=next_id;
strcpy(name,name_str);
}
static void acc_total(int f);
void print()
{
cout<<id<<endl;
cout<<next_id<<endl;
cout<<total_pay<<endl;
}
private:
char name[10];
int id;
static int next_id;
static int total_pay;
};
int employee::next_id=0;
int employee::total_pay=0;
void employee::acc_total(int f)
{
total_pay+=f;
}
int main()
{
char name1[10]="atjing";
char name2[10]="5i5j";
employee a(name1);
employee::acc_total(int a=2543);
a.print();
employee b(name2);
employee::acc_total(int pay2=3780);
b.print();
return 0;
}
#include <iostream>
using namespace std;
class employee
{
public:
employee(char* name_str)
{
next_id++;
id=next_id;
strcpy(name,name_str);
}
static void acc_total(int f);
void print()
{
cout<<id<<endl;
cout<<next_id<<endl;
cout<<total_pay<<endl;
}
private:
char name[10];
int id;
static int next_id;
static int total_pay;
};
int employee::next_id=0;
int employee::total_pay=0;
void employee::acc_total(int f)
{
total_pay+=f;
}
int main()
{
char name1[10]="atjing";
char name2[10]="5i5j";
employee a(name1);
employee::acc_total(int a=2543);
a.print();
employee b(name2);
employee::acc_total(int pay2=3780);
b.print();
return 0;
}
int main()
{
char name1[10]="atjing";
char name2[10]="5i5j";
employee a(name1);
int aa=2543; //这,已经有过employee a了,再 int a肯定不行
employee::acc_total(aa);
a.print();
employee b(name2);
int pay2=3780; //这
employee::acc_total(pay2);
b.print();
return 0;
}
---------------------------------------------
编译运行都没有问题。。。
char name1[10]="atjing";
char name2[10]="5i5j";
这两句是错的。
数组怎么初始化?好好补补。
char name1[10] ={'a','t','j','i','n','g'};
char name2[10]={'5','i','5','j'};
或者:
char name1[]="atjing";
char name2[]="5i5j";
char name1[10] ={'a','t','j','i','n','g'};
char name2[10]={'5','i',