队列未添加对象-C++11
Queue not adding objects - C++ 11
我有两个自定义类,Passenger
和Elevator
,包含在Passenger.h
头文件中:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Passenger
{
public:
explicit Passenger(int startTime, int startFloor, int endFloor, int waitTime){
this->startTime = startTime;
this->startFloor = startFloor;
this->endFloor = endFloor;
this->waitTime = waitTime;
}//end constructor
void increaseWait(int increase){
this->waitTime += increase;
}
int getWait(){
return this->waitTime;
}
int startTime, startFloor, endFloor, waitTime;
}; //end passenger
class Elevator
{
public:
explicit Elevator(string the){
status = "STOPPED";
passengerCount = 0;
name = the;
}//end constructor
int addtoQueue(Passenger person){
if (this->passengerCount < 8){
this->pickupQueue.push(person);
return 0;
}else{
cout << "Error -- full" << endl;
return 1;
} //end
} //end addtoQueue
void changePassengerCount(int change){
passengerCount += change;
}
string getStatus(){
return status;
}
void setStatus(string status){
status = status;
}
void addStop(int location){
stops.push(location);
}
int getStops(){
return stops.size();
}
string getName(){
return name;
}
int getRoute(){
return stops.front();
}
bool checkCount(){
if (passengerCount > 8){
return false;
}else{
return true;
}
}
int printCount(){
return passengerCount;
}
void printQueue(){
queue<int> copy = stops;
while(!copy.empty()){
cout << copy.front() << " ";
copy.pop();
}
cout << endl;
}
private:
string status;
queue<Passenger> pickupQueue;
int passengerCount;
queue<int> stops;
string name;
}; //end passenger
我正在将一个csv
文件读取到Passenger
对象的vector
中,然后我想做的是用这些Passenger
对象填充每个Elevator
对象中的一些queue
对象。然而,Elevator
类中似乎没有任何对象在实际更新。例如,passengerCount
和Queue Size
从不超过1。我不明白为什么。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include "Passenger.h"
using namespace std;
int time = 0;
int main(){
vector<Passenger> passengers;
Elevator elevatorOne = Elevator("elevatorOne");
Elevator elevatorTwo = Elevator("elevatorTwo");
Elevator elevatorThree = Elevator("elevatorThree");
Elevator elevatorFour = Elevator("elevatorFour");
vector<Elevator> elevators;
elevators.push_back(elevatorOne);
elevators.push_back(elevatorTwo);
elevators.push_back(elevatorThree);
elevators.push_back(elevatorFour);
// Code removed when building the vector of passengers by reading in the csv
for(unsigned int i = 0; i < passengers.size(); i++){
//cout << "Currently evaluating passenger: " << i << endl;
for (Elevator currElevator : elevators){
// If the elevator hasn't moved
// This is one of three logic checks, but the others have been removed at the request of SO users
if(currElevator.getStatus().compare("STOPPED") == 0 && currElevator.getStops() == 0 && currElevator.checkCount() == true){
// Add the passenger's end floor to the current elevator's queue
cout << "adding passenger " << i << " to elevator: " << currElevator.getName() << " because of condition 1 " << endl;
currElevator.addStop(passengers[i].endFloor);
currElevator.changePassengerCount(1);
cout << "Queue Size: " << currElevator.getStops() << endl;
cout << "Passenger Count: " << currElevator.printCount() << endl;
if(passengers[i].endFloor > currElevator.getRoute()){
currElevator.setStatus("MOVING UP");
} else{
currElevator.setStatus("MOVING DOWN");
}
break;
} //end if
else{
// If none of these conditions match, move on to the next elevator
continue;
} //end else
} //end looping through elevators
} //end looping through passengers
} //end main
我得到的结果如下:
adding passenger 0 to elevator: elevatorOne because of condition 1
Queue Size: 1
Passenger Count: 1
adding passenger 1 to elevator: elevatorOne because of condition 1
Queue Size: 1
Passenger Count: 1
adding passenger 2 to elevator: elevatorOne because of condition 1
Queue Size: 1
但我们期待看到:
adding passenger 0 to elevator: elevatorOne because of condition 1
Queue Size: 1
Passenger Count: 1
adding passenger 1 to elevator: elevatorOne because of condition 1
Queue Size: 2
Passenger Count: 2
adding passenger 2 to elevator: elevatorOne because of condition 1
Queue Size: 3
Passenger Count: 3
etc
为一个愚蠢的问题道歉。
更换:
for (Elevator currElevator : elevators){
带有:
for (int j = 0; j < elevators.size(); j++){
并且对CCD_ 13到CCD_。
我不确定,在这个时候,为什么会有效果,但会随着进一步的研究更新这个答案。
更新
根据Corristo:发生这种情况的原因是,基于循环的范围内的currElevator
是elevators
向量中电梯的副本。如果将基于范围的for循环声明为for(Elevator& currElevator : elevators
(,它应该可以工作,因为现在currElevator
是对elevators
向量中元素的引用,而不是副本。
相关文章:
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- C++11 中不同类型的对象的 std::array 的替代方案
- 队列未添加对象-C++11
- C++11 中的对象验证逻辑模式
- 从从可调用参数创建的线程对象参数移动构造 C++11 线程
- 为什么编译器在试图初始化具有C 11样式的对象数组时隐含删除构造函数
- 如何在 Windows 上从 C++11 统计信息对象的文件序列号中查找文件名
- 实现功能对象绑定而无需使用C 11
- 启动程序和libocci.so.11.1:无法打开共享对象文件:没有这样的文件或目录
- 子类对象列表重新解释为基类对象列表?(C++11).
- GCC:当层次结构中存在虚拟继承时,C++11 内联对象初始化(使用 "this")不起作用
- C 11是否保证将移动垂死的对象而不是复制为参数
- C++11 initializer_list不适用于嵌入对象的文字常量值?
- 在C++11中按值返回构造的对象时,避免复制构造函数调用
- 链接C++17、C++14和C++11对象是否安全
- DirectX 11 基于旋转移动对象
- IE 探索 11 < c++ ATL COM 浏览器帮助程序对象(加载项)来替换 DOM 中的文本
- DirectX 11渲染对象内部
- 我能否使 C++11 类/对象通用线程安全?
- 在动态分配时将C++11对象添加到列表中