Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no viable overloaded '='

Tags:

c++

syntax

I'm taking a simulation course in C++ right now, and am getting the clang++ error quoted in the title. I was hoping someone could tell me why because I cannot seem to find a similar error for a similar situation anywhere (search as I may).

The error occurs for each Office* variable definition (lines 187 to 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 instance), and change aid = new Office(15, Tf) to Office* aid = new Office(15, Tf), the error goes away. I have no idea why, and would very much like to, because I want all of these class pointers to be private.

Interestingly (irritatingly?), the MinHeap* time_heap; time_heap = new MinHeap(); does not cause any problems. I thought it may have to do with declaring a pointer var as private then defining it in the public portion of the class but it looks like no.

help? =|

like image 491
user1647959 Avatar asked Feb 20 '23 06:02

user1647959


2 Answers

Office* aid, bursar, registrar, parking;

Declares a single pointer, and 3 objects. You probably think you want:

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

And you actually want:

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

and to initialize them in the constructor initializer list. If the class isn't the resource owner, go with std::shared_ptr.

like image 147
Luchian Grigore Avatar answered Feb 27 '23 00:02

Luchian Grigore


Here:

Office* aid, bursar, registrar, parking;

only aid is an Office*, the rest are Office. Looking at your code, It looks like you can easily avoid problems by not 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 designed to be only called once. You are probably better off initializing in the constructor.

EventHandler::EventHandler() 
 : aid(8,Tf), bursar(15, Tb), registrar(5, Tr), parking(10, Tp) {}
like image 29
juanchopanza Avatar answered Feb 26 '23 23:02

juanchopanza