bool:不正确的cout输出,而不是0或1

Bool: Incorrect output of cout not 0 or 1

本文关键字:不正确 cout 输出 bool      更新时间:2023-10-16

我的功能在请求中读取:按时间戳,当前的地板和目的地地板,它没有输出我的预期。

我所有的成员值都正确输出:时间戳,当前地板和目的地地板,除了布尔。

布尔输出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

2050xCD。这通常意味着您使用的是非初始化的变量。

基于原始问题中的代码,您需要在reqNode的复制构造函数中复制direction。根据输出,未复制。

另外,由于您的向量似乎是vector<reqNode>,因此您无需将临时reqNode分配给new。只需在堆栈上创建它,然后将其传递给requests.push_back

request.push(*temp)制作了temp指向的对象的副本。作为样式,您应该在执行此操作后删除指针,因为不再需要该对象。更好的是,将其作为自动对象而不是新启动。C 不是Java。

由于存储的副本与原始的值不同,因此表明reqNode的复制构造函数未正确复制。