bool:不正确的cout输出,而不是0或1
Bool: Incorrect output of cout not 0 or 1
我的功能在请求中读取:按时间戳,当前的地板和目的地地板,它没有输出我的预期。
我所有的成员值都正确输出:时间戳,当前地板和目的地地板,除了布尔。
布尔输出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
。这通常意味着您使用的是非初始化的变量。
基于原始问题中的代码,您需要在reqNode
的复制构造函数中复制direction
。根据输出,未复制。
另外,由于您的向量似乎是vector<reqNode>
,因此您无需将临时reqNode
分配给new
。只需在堆栈上创建它,然后将其传递给requests.push_back
。
request.push(*temp)
制作了temp
指向的对象的副本。作为样式,您应该在执行此操作后删除指针,因为不再需要该对象。更好的是,将其作为自动对象而不是新启动。C 不是Java。
由于存储的副本与原始的值不同,因此表明reqNode
的复制构造函数未正确复制。
相关文章:
- 使用2个键的cpp-stl::优先级队列排序不正确
- 正弦级数方程计算不正确
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 密码登录程序将永远循环并显示不正确的结果
- 在C++中返回不正确的楼层函数值
- 形状对象的旋转和缩放不正确C++
- C++ 读取文件读取文件不正确
- 关于类的 Python 文档 - 对C++的引用不正确
- 特征 LLT 模块给出不正确的结果?
- glibcxx STL 在实现 std::valarray::sum() 时是否不正确?
- 如果语句逻辑不正确
- 来自逆 vp 矩阵和相机位置的光线方向不正确
- 不正确的操作数类型 MSVC
- 数组填充了不正确的值
- 我是否不正确地集中了这些字符数组?
- 为什么除非我使用 cout,否则我的值不正确?
- 输出使用 cout 转换为 ASCII 的文本时输出格式不正确,C++
- C++ cout 在擦除换行符后打印字符串不正确
- cout 打印的值不正确
- bool:不正确的cout输出,而不是0或1