No viable overloaded '='

I am taking a C ++ modeling course right now and I am getting the clang ++ error indicated in the title. I was hoping someone would tell me why, because I cannot find a similar error for a similar situation anywhere (search how I can).

An error occurs for each Office* variable definition (lines 187 through 190).

 175 class EventHandler { 176 177 private: 178 double simulation_time; // current simulation time 179 Office* aid, bursar, registrar, parking; 180 Event* current_event; 181 MinHeap* time_heap; 182 183 public: 184 185 void initialize_simulation() { // initializes simulation time, offices, and event handler (time_heap) 186 simulation_time = 0; 187 aid = new Office(8, Tf); // initializes financial aid office with Tf tellers, with serve time exponentially distributed with mean of 8 minutes 188 bursar = new Office(15, Tb); // initializes bursar office w/ Tb tellers, exp distro, mean 15 min 189 registrar = new Office(5, Tr); // registrar w/ Tr tellers, exp distro, mean 5 min 190 parking = new Office(10,Tp); // parking office w/ Tp tellers, exp distro, mean 10 192 MinHeap* time_heap = new MinHeap(); 193 } 

If I replace the Office* aid declaration (for example) and change aid = new Office(15, Tf) to Office* aid = new Office(15, Tf) , the error will disappear. I don’t know why, and I would really like it because I want all these class pointers to be private .

Interesting (annoying?), MinHeap* time_heap; time_heap = new MinHeap(); MinHeap* time_heap; time_heap = new MinHeap(); does not cause any problems. I thought this could be due to declaring the var pointer as private , and then defining it in the public part of the class, but it looks like it isn't.

help? = |

+4
source share
2 answers
 Office* aid, bursar, registrar, parking; 

Declares one pointer and 3 objects. You probably think:

 Office *aid, *bursar, *registrar, *parking; 

And you really want:

 std::unique_ptr<Office> aid; std::unique_ptr<Office> busar; std::unique_ptr<Office> parking; std::unique_ptr<Office> registrar; 

and initialize them in the constructor initializer list. If the class does not own the resource, go to std::shared_ptr .

+6
source

Here:

 Office* aid, bursar, registrar, parking; 

only aid is Office* , the rest is Office . Look at your code. It looks like you can easily avoid the problems without using pointers:

 Office aid, bursar, registrar, parking; 

then

  aid = Office(8, Tf); bursar = Office(15, Tb); registrar = Office(5, Tr); parking = Office(10,Tp); 

Also, your initialize_simulation() seems to be designed to be called only once. You should probably start initializing in the constructor.

 EventHandler::EventHandler() : aid(8,Tf), bursar(15, Tb), registrar(5, Tr), parking(10, Tp) {} 
+2
source

Source: https://habr.com/ru/post/1432408/


All Articles