C++ 奇怪的行为c++;for开关语句
在过去的3个小时里,我一直在看这段代码,我被卡住了。谢谢你的帮助,谢谢 文件:UnsortedType.hC++ 奇怪的行为c++;for开关语句,c++,switch-statement,C++,Switch Statement,在过去的3个小时里,我一直在看这段代码,我被卡住了。谢谢你的帮助,谢谢 文件:UnsortedType.h #include "ItemType.h" class UnsortedType{ public: UnsortedType(); void RetireveItem(ItemType& item, bool& found); bool InsertItem(ItemType item); private: int length;
#include "ItemType.h"
class UnsortedType{
public:
UnsortedType();
void RetireveItem(ItemType& item, bool& found);
bool InsertItem(ItemType item);
private:
int length;
ItemType info[MAX_ITEMS];
};
文件:UnsortedType.cpp
#include "UnsortedType.h"
#include <iostream>
UnsortedType::UnsortedType() {
length = 0;
}
void UnsortedType::RetireveItem(ItemType& item, bool& found) {
bool moreToSearch = true;
int location = 0;
found = false;
moreToSearch = (location < length);
while (moreToSearch && !found) {
switch (item.ComparedTo(info[location])) {
case LESS:
location++;
moreToSearch = (location < length);
break;
case GREATER:
location++;
moreToSearch = (location < length);
break;
case EQUAL:
found = true;
break;
}
}
if (found) {
item = info[location];
std::cout << "Item " << item.getValue() << " has been retrieved." << std::endl;
}
else {
std::cout << "Item " << item.getValue() << " has NOT found and has NOT been retrieved."
}
}
bool UnsortedType::InsertItem(ItemType item) {
if (length == MAX_ITEMS) {
std::cout << "List is Full!" << std::endl;
std::cout << "Item " << item.getValue() << " has not been added." << std::endl;
return false;
} else {
std::cout << "Item " << item.getValue() << " added successfully." << std::endl;
info[length] = item;
length++;
return true;
}
}
文件:ItemType.cpp
ItemType::ItemType(){
this->value=0;
}
ItemType::ItemType(int value){
this->value = value;
}
RelationType ItemType::ComparedTo(ItemType otherItem){
if(value < otherItem.value){
return LESS;
}
if(value == otherItem.value){
return EQUAL;
}
if(value < otherItem.value){
return GREATER;
}
}
void ItemType::Initialize(int value){
this->value = value;
}
void ItemType::printItem(){
std::cout << "Item Type: " << this->value <<std::endl;
}
int ItemType::getValue(){
return this->value;
}
没有问题
输出为:
Item 3 added successfully.
Item 3 has been retrieved.
Item 1 added successfully.
Item 1 has been retrieved.
Item 2 has NOT found and has NOT been retrieved.
但是,如果我首先添加item1并检索item1,然后添加item3并检索item3,switch语句会突然停止工作
这是奇怪情况下的主文件:
UnsortedType unsortedType;
bool item1found = false;
ItemType item1(1);
unsortedType.InsertItem(item1);
unsortedType.RetireveItem(item1, item1found);
bool item3found = false;
ItemType item3(3);
unsortedType.InsertItem(item3);
unsortedType.RetireveItem(item3, item3found);
bool item2found = false;
ItemType item2(2);
unsortedType.RetireveItem(item2, item2found);
Item 1 added successfully.
Item 1 has been retrieved.
Item 3 added successfully.
RUN FAILED (exit value 1, total time: 1s)
调试程序时,我发现:
while(moreToSearch&!found)
代码不会转到任何switch语句。有什么想法吗
这是奇怪情况下的输出:
UnsortedType unsortedType;
bool item1found = false;
ItemType item1(1);
unsortedType.InsertItem(item1);
unsortedType.RetireveItem(item1, item1found);
bool item3found = false;
ItemType item3(3);
unsortedType.InsertItem(item3);
unsortedType.RetireveItem(item3, item3found);
bool item2found = false;
ItemType item2(2);
unsortedType.RetireveItem(item2, item2found);
Item 1 added successfully.
Item 1 has been retrieved.
Item 3 added successfully.
RUN FAILED (exit value 1, total time: 1s)
任何帮助都被占用了,我就要失去它了 与成员函数相比,问题似乎出在您的
中:
RelationType ItemType::ComparedTo(ItemType otherItem){
if(value < otherItem.value){
return LESS;
}
if(value == otherItem.value){
return EQUAL;
}
if(value < otherItem.value){
return GREATER;
}
}
RelationType ItemType::ComparedTo(ItemType otherItem){
如果(值<其他项值){
回报少;
}
如果(值==otherItem.value){
回报相等;
}
如果(值<其他项值){
回报更大;
}
}
较大的
案例的比较似乎不正确。对于这样一个函数,不允许通过它的可能路径不返回值是有意义的(即,使用if
,else if
,else
)。此外,您可能希望打开所有编译器警告并将其视为错误;这将有助于避免类似的问题。RelationType ItemType::ComparedTo(ItemType otherItem){
RelationType ItemType::ComparedTo(ItemType otherItem){
if(value < otherItem.value){
return LESS;
}
if(value == otherItem.value){
return EQUAL;
}
if(value < otherItem.value){ // !!!!
return GREATER;
}
}
如果(值<其他项值){
回报少;
}
如果(值==otherItem.value){
回报相等;
}
如果(值
与
功能比较的不正确。如果value>otherItem.value
它将不会输入任何If
s,并且它将在不返回值的情况下脱落,从而导致未定义的行为。与方法相比,最后一个If
语句与第一个语句相同-它应该具有
而不是otherItem.value)
回报更大;
回报相等;
}
(a) 实际上,我可能已经重写了
和=
操作符(以及其他必要的操作符),这样我就可以在代码中编写if(a
,而不是if(a.comparedTo(b)==LESS)
。但这可能是您教育的下一步:-)我建议在switch
语句中添加default:
子句,继续调试。让它打印出一些荒谬的语句,其中包括您正在打开的变量的值。谢谢,有没有办法将问题标记为已解决?@KorayTugay-如果您认为其中一个答案是正确的,请选择它。眼睛真好!复制粘贴超光速。谢谢,我正要松开它。复制和粘贴ftl是什么意思?@KorayTugay我可能做出了一个不明智的假设,即您复制和粘贴了if
条件,但没有更改其中一个条件。谢谢,也许。。。我真的不记得了。疯狂的时间,经验+0.00001。我想比0强。。
RelationType ItemType::ComparedTo (ItemType otherItem) {
if (value < otherItem.value)
return LESS;
if (value > otherItem.value)
return GREATER;
return EQUAL;
}