我创建了一个名为' employee' ,我希望类的对象在创建时调用参数化构造函数employee::employee(char* name, int id)
,并且对象被声明为对象数组employee emp[]
,以下代码表示如此。
我尝试使用以下语法:
for(int x=0; x<10; x++) //say for 10 objects of employee class
{ cin>>l>>b>>h;
employee emp[x]={employee(l,b,h)}; //i don't whether its correct or not
}
但此代码出现以下错误:
//错误:第29行:需要常量表达式
以下是我编译的代码:
#include<iostream.h>
#include<conio.h>
class employee
{
char* name;
int eid;
public:
employee()
{ }
employee(char* name, int eid)
{
this->name=name;
this->eid=eid;
}
void display()
{
cout<<name<<" "<<eid<<"\n";
}
};
int main()
{ char *name;
int id;
cout<<"ENTER NAME AND EID OF 10 EMPLOYEES:";
for(int x=0; x<10; x++)
{ cout<<"enter name:\n" ;
cin>>name;
cout<<"enter id:\n";
cin>>id;
employee emp[x]={employee(name,id)}; //line 29
}
return 0;
}