在过去的 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 )等等。
现在问题来了:
当我像这样运行主程序时:
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)
任何大大挪用的帮助,我都快失去它了!