gpt4 book ai didi

c++ - switch 语句的奇怪行为 c++

转载 作者:太空宇宙 更新时间:2023-11-04 15:06:34 25 4
gpt4 key购买 nike

<分区>

在过去的 3 个小时里,我一直在查看这段代码,但我被困住了。感谢您的帮助,谢谢。

文件:UnsortedType.h

#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.h

const int MAX_ITEMS = 40;
enum RelationType{LESS,GREATER,EQUAL};

class ItemType{

private:
int value;

public:
ItemType();
ItemType(int value);
RelationType ComparedTo(ItemType otherItem);
void Initialize(int value);
void printItem();
int getValue();
};

文件: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;
}

请注意:在上面的代码中,我省略了一些我认为不相关的代码部分。因此,如果您复制/粘贴代码并运行它可能需要一些包含语句(如 iostream)等等......

问题来了:

当我像这样运行 main 时:

UnsortedType unsortedType;

bool item3found = false;
ItemType item3(3);
unsortedType.InsertItem(item3);
unsortedType.RetireveItem(item3, item3found);

bool item1found = false;
ItemType item1(1);
unsortedType.InsertItem(item1);
unsortedType.RetireveItem(item1, item1found);

bool item2found = false;
ItemType item2(2);
unsortedType.RetireveItem(item2, item2found);

没有问题。

输出是:

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);

在调试程序时,我不断发现: while (moreToSearch && !found)并且代码不会转到任何 switch 语句。有什么想法吗?

这是奇怪情况下的输出:

Item 1 added successfully.
Item 1 has been retrieved.
Item 3 added successfully.

RUN FAILED (exit value 1, total time: 1s)

大大挪用的任何帮助,我都快要失去了!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com