我现在正在使用C++进行模拟课程,并且在标题中引用了clang ++错误.我希望有人可以告诉我为什么,因为我似乎无法在任何地方找到类似的错误(尽可能搜索).
每个Office*变量定义都会发生错误(第187到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 }
Run Code Online (Sandbox Code Playgroud)
如果我替换Office* aid声明(例如),并更改aid = new Office(15, Tf)为Office* aid = new Office(15, Tf),则错误消失.我不知道为什么,也非常愿意,因为我想要所有这些类指针private.
有趣的是(恼人的?),MinHeap* time_heap; time_heap = new MinHeap();不会造成任何问题.我认为它可能与声明指针var private 然后public在类的部分中定义它但它看起来像没有关系.
救命?= |
Office* aid, bursar, registrar, parking;
Run Code Online (Sandbox Code Playgroud)
声明一个指针和3个对象.你可能认为你想要:
Office *aid, *bursar, *registrar, *parking;
Run Code Online (Sandbox Code Playgroud)
你真的想要:
std::unique_ptr<Office> aid;
std::unique_ptr<Office> busar;
std::unique_ptr<Office> parking;
std::unique_ptr<Office> registrar;
Run Code Online (Sandbox Code Playgroud)
并在构造函数初始化列表中初始化它们.如果该类不是资源所有者,请使用std::shared_ptr.
| 归档时间: |
|
| 查看次数: |
13240 次 |
| 最近记录: |