Constructors in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Constructors in C++

Why I face this Error, Again and again, I tried to solve out but I didn't is anyone help me with this:------ #include<iostream> #include<string.h> #include<conio.h> using namespace std; class employees{ private: int emp_no; char emp_name[20]; char emp_address[30]; int no_of_dependents; public: employees(){emp_no=no_of_dependents=0;strcpy(emp_name,0);strcpy(emp_address,0);} void setEmp_no(int t){emp_no=t; } void setEmp_name(string a){a=emp_name; } void setEmp_address(string a){a=emp_address; } void setNo_of_dependents(int s){no_of_dependents=s; } void input(){cin>>emp_no>>emp_name>>emp_address>>no_of_dependents; } void getEmployee_no(){emp_no;} string getEmployee_name(){return emp_name;} string getEmployee_address(){return emp_address;} void getNo_of_dependents(){no_of_dependents;} void display(){cout<<emp_no<<emp_name<<emp_address<<no_of_dependents;} }; int main() { employees empno,ename[20],eadd[30],nod; empno.input(); ename.input(); eadd.input(); nod.input(); empno.display(); ename.display(); eadd.display(); nod.display(); getch(); }

18th Aug 2018, 5:31 PM
MANISH JHA
MANISH JHA - avatar
2 Answers
+ 1
Manish Jha Where are you compiling the code? On Sololearn or on a PC? Also, I see some logical errors which I will mention below, and maybe correcting those will help you : 0) In setEmp_Name you are accepting an std::string (a in this case), but in the function body, you reassign a with the value of emp_name, and not the other way around. That may be a logical error. I think you should do : strcpy(a.c_str(),emp_name); and include the header <string> for std::string. 1) In some getter functions, you specified the return type as void, but I think they are expected to return the appropriate values that you have mentioned inside the same. That is also a logical_error.
18th Aug 2018, 5:48 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
yes, I compiled this program in codeblocks but I face the problem with strings in the constructor.
19th Aug 2018, 8:29 AM
MANISH JHA
MANISH JHA - avatar