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.

  • It is necessary to implement the function driver::driver() - VTT

1 answer 1

It is quite clear because it says - no driver::driver(); . You have declared the default constructor driver(); but the bodies did not write ...

You also have some left names in

  prename=cop.surname; categorii=cop.categories; 

There are no such members in the class.

In reads() , n and p not declared.

Well, and in

 driver a=new driver[nr]; 

you are missing an asterisk. This is what concerns the syntax.

What do you mean by "dynamic array"? If the array is selected in the heap, then you have already received it (as soon as you finish the default constructor). I strongly advise malloc - it’s just a memory allocation, constructors / destructors will not be called, and in general, this is not our (C ++) method :)

If an array that dynamically changes its size at runtime, then it’s still hands-on — judging by your code — is still a bit early; it makes sense to use vector .

  • prename=cop.surname; categorii=cop.categories; n and p in reads() corrected in the code, made a mistake when translating to make it clear. Added {} to driver() and it worked. Hooray! As for the array, I want to create an array with char data with "categories" of water rights for each driver. Now I can enter only one. That in reads() asked the number of available categories, and after that the creation of an array with memory allocation and query the categories themselves. - Ion Crismaru
  • BUT! Well, since the category is just a Latin letter, I would just hold the category string - you have it, and so is the string. Add the necessary letters to it and do not suffer ... The string is a dynamic array of char elements :) - Harry
  • And yet - if the answer to the question suits - then the question is closed, accepting the answer (the bird to the left of it). - Harry
  • Thanks a lot for your help! Close - Ion Crismaru
  • And yet - you completely in vain removed from the question all the errors and the error message itself! At a minimum, it turns out that everything is all right with you, but some assholes :) write about errors that seem to be absent. The question must remain the original question! It can be updated, but not changed! - Harry