切换语句的奇怪行为c ++

时间:2012-10-29 21:34:24

标签: 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];
};

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

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

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

请注意:在上面的代码中,我已经省略了一些我认为不相关的代码部分。因此,如果您复制/粘贴代码并运行它可能需要一些包含语句(如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&amp;&amp;!found) 并且代码不会转到任何switch语句。有什么想法吗?

这是奇怪情况下的输出:

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

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

任何帮助大大占用,我即将失去它!

3 个答案:

答案 0 :(得分:5)

问题似乎出现在ComparedTo成员函数中:

RelationType ItemType::ComparedTo(ItemType otherItem){

    if(value < otherItem.value){
        return LESS;
    }

    if(value == otherItem.value){
        return EQUAL;
    }

    if(value < otherItem.value){
        return GREATER;
    }    

}

GREATER案例的比较似乎不正确。对于这样的函数,有意义的是,您不允许通过它的可能路径不会返回值(即,使用ifelse ifelse)。此外,您可能希望打开所有编译器警告并将其视为错误;这将有助于避免这样的问题。

答案 1 :(得分:2)

comparedTo方法中,最后一个if语句与第一个语句相同 - 它应该是>而不是<

事实上,我很惊讶你的编译器没有抱怨代码路径没有为该函数返回值的可能性。如果是这种情况,您可能需要考虑调高警告级别 - 否则,监听到您的编译器,它通常不会告诉您不需要知道的东西: - )

我会在那时无条件地返回GREATER (a),因为如果它既不是更小也不相等,那是唯一剩下的可能性。换句话说,比如:

RelationType ItemType::ComparedTo (ItemType otherItem) {
    if (value < otherItem.value)
        return LESS;

    if (value > otherItem.value)
        return GREATER;

    return EQUAL;
}

(a)实际上,我可能会覆盖<>==运算符(以及其他必要的运算符),所以我可以写我的代码中的if (a < b),而不是if (a.comparedTo(b) == LESS)。但这可能是你教育的下一步: - )

答案 2 :(得分:1)

RelationType ItemType::ComparedTo(ItemType otherItem){
    if(value < otherItem.value){
        return LESS;
    }
    if(value == otherItem.value){
        return EQUAL;
    }
    if(value < otherItem.value){         // !!!!
        return GREATER;
    }    
}

ComparedTo功能不正确。如果value > otherItem.value它不会进入任何if,它将在不返回值的情况下脱落,从而导致未定义的行为。