0

I have created a class named 'employee' , and i want the object of the class to invoke the parameterized constructor employee::employee(char* name, int id) when they are created,and the objects were declared as array of objects employee emp[], the following code represent so. I tried using the following syntax:

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 }

but this code is giving the following error:

//error: line 29: Constant expression required

Following is the code which I compiled:

#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;

}

KidWithAComputer
  • 311
  • 1
  • 2
  • 10

0 Answers0