Kor*_*gay 0 c++ switch-statement
过去3个小时我一直在看这段代码而且我很困惑.我感谢任何帮助,谢谢.
file: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];
};
Run Code Online (Sandbox Code Playgroud)
file: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;
}
}
Run Code Online (Sandbox Code Playgroud)
file: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();
};
Run Code Online (Sandbox Code Playgroud)
file: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;
}
Run Code Online (Sandbox Code Playgroud)
请注意:在上面的代码中,我已经省略了一些我认为不相关的代码部分.因此,如果您复制/粘贴代码并运行它可能需要一些包含语句(如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);
Run Code Online (Sandbox Code Playgroud)
没有问题.
输出是:
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.
Run Code Online (Sandbox Code Playgroud)
但是,如果我首先添加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);
Run Code Online (Sandbox Code Playgroud)
在调试程序的过程中,我一直在寻找: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)
Run Code Online (Sandbox Code Playgroud)
任何帮助大大挪用,我即将失去它!
问题似乎在您的ComparedTo成员函数中:
RelationType ItemType::ComparedTo(ItemType otherItem){
if(value < otherItem.value){
return LESS;
}
if(value == otherItem.value){
return EQUAL;
}
if(value < otherItem.value){
return GREATER;
}
}
Run Code Online (Sandbox Code Playgroud)
GREATER案件的比较似乎不正确.对于这样的功能,它将使意义,你不允许通过它的可能路径,将不返回值(即,使用if,else if,else).此外,您可能希望打开所有编译器警告并将其视为错误; 这将有助于避免这样的问题.
| 归档时间: |
|
| 查看次数: |
404 次 |
| 最近记录: |