Wrote code, but in line
driver *a=new driver[nr] gives an error message:
undefined reference to driver :: driver ();
#include <iostream> #include <string> #include <stdlib.h> using namespace std; class driver { private: string name; string surname; string categories; int salary, hours; public: driver(); ~driver() { std::cout<<"\nDestructor executed"; } driver(driver const &cop) { name=cop.name; prename=cop.surname; categorii=cop.categories; salary=cop.salary; hours=cop.hours; } driver(string n, string p, string c, int s, int h) { name.clear(); surname.clear(); categories.clear(); salary=0; hours=0; init(n,p,c,s,h); } void init(string n, string p, string c, int s, int h) { name=n; surname=p; categories=c; salary=s; hours=h; } void reads() { std::cout<<"\t\t Give information about driver:"<<std::endl; std::cout<<"\t\t---------------------------------------\n"; std::cout<<"\tGive name: "; std::cin>>name; std::cout<<"\tGive surname: "; std::cin>>surname; std::cout<<"\tGive categories of driver license: "; std::cin>>categories; std::cout<<"\tHow much he is payd for hour: "; std::cin>>salary; std::cout<<"\tHow many hours did "<<n<<" "<<p<<" works? "; std::cin>>hours; } void print() { std::cout<<name<<" "<<surname<<" "; std::cout<<"has categories "<<categories<<endl; std::cout<<"Salary per hour is "<<salary<<endl; std::cout<<"Driver had worked "<<hours<<" hours"<<endl; std::cout<<"Full payment is "<<salary*hours<<" $"<<endl; } }; int main() { int nr,i; cout<<"Vvedite cislo voditeley:"; cin>>nr; driver a=new driver[nr]; for(i=0;i<nr;i++) { a[i].reads(); cout<<endl; } for(i=0;i<nr;i++) { a[i].print(); cout<<endl; } delete[] a; return 0; } What is wrong in this case?
And also how to make a dynamic array? I tried in C:
a=(driver*) malloc (nr*sizeof(driver)) but fails.
driver::driver()- VTT