c ++成员变量未在函数外部更改

时间:2013-02-01 19:28:40

标签: c++

附加代码似乎可以正常工作,但是当它在单独的文件main.cpp中时,Set.h,Set.cpp fill不会在remove函数之外递减。我真的不明白这里发生了什么。或者为什么它会在一个文件中产生任何差异。

#include <iostream>
using namespace std;
typedef int value_type;

class Set {
private:
value_type *dataArray, size, filled;
public:

Set() {
    size = 50;
    dataArray = new value_type[size];
    filled = 0;
}

bool Set::isFull() const {
    return (filled == size) ? true : false; // if filled is equal to size then full.
}

bool Set::remove(const value_type& item) {

    for (int index = 0; index < filled; index++) {
        if (index != (filled - 1) && dataArray[index] == item) {
            dataArray[index] = dataArray[filled - 1];
            --filled;
            return true;

        } else {
            --filled;
            return true;
        }
    }

    return false;
}

void Set::insert(const value_type& newItem) {

    if (!isFull()) {
        dataArray[filled] = newItem;

        filled++; // increment filled to account for new entry.

    }
}

friend ostream& operator<<(ostream& out, const Set& obj) {
    out << "\nfilled: " << obj.filled << endl;
    out << "{";

    for (int index = 0; index < obj.filled; index++) {
        out << obj.dataArray[index];
        if (index != (obj.filled - 1))
            cout << ",";

    }
    out << "}";
    return out;
}
};
Set firstSet;

void pauseNwait() {
cout << "<--Enter to Continue-->";
cin.ignore();
cin.get();
}

int main() {

int choice = -1;
value_type input;

while (choice != 0) {
    cout << "       Set Manager" << endl
            << " (1) Add item to Set 1" << endl
            << " (2) Remove item from Set 1" << endl
            << " (0) Exit" << endl
            << "-----------------------------------------" << endl
            << "Choose: ";
    cin.clear();

    if (cin >> choice) {
        switch (choice) {
            case 0:
                // Exit.
                break;
            case 1:
                cout << "Enter int to add to list: ";
                cin >> input;
                firstSet.insert(input);
                cout << "First Set: " << firstSet << endl;
                pauseNwait();
                break;
            case 2:
                cout << firstSet << endl;
                cout << "Enter item to remove from list: ";
                cin >> input;
                firstSet.remove(input);
                cout << "First Set: " << firstSet << endl;
                pauseNwait();
                break;
            default:
                break;
        }
    } else {
        cin.clear(); // clear cin to avoid invalid menu input errors.
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}
return 0;
}

2 个答案:

答案 0 :(得分:1)

如果 dataArray 是该类的成员,那么您所看到的可能是在数组边界外指定值的副作用。

将会发生的事情是,该值将被设置到相邻的类成员上,您不会得到任何异常,因为内存确实属于您的程序(它仍然在类中)。

情景:

循环遍历数组,找不到任何内容 现在索引超出界限 您检查该位置的项目是否等于项目。 它可能等于item。 然后你将数组中的“last”项复制到那个地方。

答案 1 :(得分:1)

我想出了问题感谢人们迫使我在一个文件中制作一个较小的版本。在原始程序中我有多个操作对象,所以我在main.cpp中创建了一个函数,它可以处理函数参数中提供的不同对象。这样做我将对象的副本传递给辅助函数而不是引用。插入函数工作正常的原因是因为我已经作为参考传递了。感谢您的帮助,如果我从这次经历中学到了什么,那就是我应该提供最小化的完全可编译的代码,这将来可能会阻止我寻求帮助。

相关问题