简体   繁体   English

布尔:错误的cout输出不为0或1

[英]Bool: Incorrect output of cout not 0 or 1

I have a function that reads in requests: by timestamp, current floor, and destination floor and it is not outputting the way I expected. 我有一个读取请求的函数:按时间戳,当前楼层和目标楼层,并且它没有以我期望的方式输出。

All my member values are outputting correctly : timestamp, current floor, and destination floor except for the bool. 我的所有成员值都正确输出:时间戳,当前楼层和目标楼层(布尔值除外)。

The bool outputs 205 instead 1 or 0 for my direction. 布尔输出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++;
}


}

I am trying to get the output: 我正在尝试获取输出:

Node # : Timestamp. 节点编号:时间戳。 Current(User Floor). 当前(用户层)。 Destination(User Floor). 目的地(用户层)。 Direction(User is heading). 方向(用户正在前进)。

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

Instead I am getting as output: 相反,我得到的输出:

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

this is the file T1.txt: 这是文件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 is 0xCD . 2050xCD That usually means that you are using an uninitialized variable. 这通常意味着您正在使用未初始化的变量。

Based on the code in the original question, you need to copy direction in the copy constructor of reqNode . 根据原始问题中的代码,您需要在reqNode的复制构造函数中复制direction Based on the output, it was not copied. 根据输出,它没有被复制。

Also, since your vector appears to be vector<reqNode> , you don't need to allocate the temporary reqNode with new . 另外,由于您的向量似乎是vector<reqNode> ,因此您无需为new分配临时的reqNode Just create it on the stack and pass that to requests.push_back . 只需在堆栈上创建它,然后将其传递给requests.push_back

request.push(*temp) makes a copy of the object that temp points to. request.push(*temp)复制temp指向的对象。 As a matter of style, you should delete the pointer after doing this, since that object is no longer needed. 作为样式,您应该在执行此操作后删除指针,因为不再需要该对象。 Even better, create it as an auto object instead of newing it. 更好的是,将其创建为自动对象,而不是对其进行更新。 C++ isn't Java. C ++不是Java。

Since the copy that's getting stored has different values from the original, it suggests that the copy constructor for reqNode isn't copying correctly. 由于要存储的副本具有与原始副本不同的值,因此表明reqNode的副本构造函数未正确复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM