我有一个读取请求的函数:按时间戳、当前楼层和目标楼层,但它没有按我预期的方式输出。
我的所有成员值都正确输出:时间戳、当前楼层和目标楼层, boolean 值除外。
boolean 输出 205 而不是我的方向 1 或 0。
Elevator::readRequests()
{
ifstream myStream("T1.txt");
while(!myStream.eof())
{
int timestamp ,currentFloor, destinationFloor;
myStream >> timestamp >> currentFloor >> destinationFloor;
//cout<< endl <<"The current timestamp is "<< timestamp << "The current floor is " << currentFloor
// << " and the destination floor is " << destinationFloor << endl << endl;
//cout<< endl;
reqNode *temp = new reqNode;
//initialize request node object
temp->timestamp = timestamp;
temp->start = currentFloor;
temp->destination = destinationFloor;
temp->start_time = -1;
temp->finish_time = -1;
temp->calculate_priority();
if(temp->start < temp->destination)
temp->set_dir(true);
else
temp->set_dir(false);
request.push(*temp);//push nodes into the request bank
}
int i = 0;
while( !request.empty() )
{
cout << "Node " << i << " : " << request.front().timestamp << " " << request.front().start << " " << request.front().destination
<< " " << request.front().direction << endl;
request.pop();//popping the request in order to test
i++;
}
}
我正在尝试获取输出:
节点#:时间戳。当前(用户楼层)。目的地(用户楼层)。方向(用户的方向)。
Node 0 : 1 3 7 1
Node 1 : 1 2 9 1
Node 2 : 1 7 9 1
Node 3 : 2 4 6 1
Node 4 : 2 4 8 1
Node 5 : 2 1 17 1
Node 6 : 5 1 15 1
Node 7 : 5 5 1 0
Node 8 : 6 17 4 0
Node 9 : 6 4 17 1
相反,我得到的是输出:
Node 0 : 1 3 7 205
Node 1 : 1 2 9 205
Node 2 : 1 7 9 205
Node 3 : 2 4 6 205
Node 4 : 2 4 8 205
Node 5 : 2 1 17 205
Node 6 : 5 1 15 205
Node 7 : 5 5 1 205
Node 8 : 6 17 4 205
Node 9 : 6 4 17 205
这是文件 T1.txt:
1 3 7
1 2 9
1 7 9
2 4 6
2 4 8
2 1 17
5 1 15
5 5 1
6 17 4
6 4 17
最佳答案
205
是0xCD
.这通常意味着您正在使用未初始化的变量。
根据原题中的代码,需要复制direction
在 reqNode
的复制构造函数中.根据输出,它没有被复制。
此外,由于您的 vector 似乎是 vector<reqNode>
, 你不需要分配临时 reqNode
与 new
.只需在堆栈上创建它并将其传递给 requests.push_back
.
关于c++ - boolean : Incorrect output of cout not 0 or 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13144701/