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

C++ c++;未在函数外更改成员变量,c++,C++,附加的代码似乎可以按预期工作,但是当它位于单独的文件main.cpp、Set.h、Set.cpp filled中时,它不会在remove函数之外递减。我真的不明白这里发生了什么。或者为什么它会有任何不同,它都在一个文件中 #include <iostream> using namespace std; typedef int value_type; class Set { private: value_type *dataArray, size, filled; public:

附加的代码似乎可以按预期工作,但是当它位于单独的文件main.cpp、Set.h、Set.cpp filled中时,它不会在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;
}
#包括
使用名称空间std;
typedef int value_type;
类集{
私人:
值\类型*数据数组,大小,已填充;
公众:
集合(){
尺寸=50;
dataArray=新值_类型[大小];
填充=0;
}
布尔集::isFull()常量{
return(filled==size)?true:false;//如果filled等于size,则为full。
}
布尔集合::删除(常量值\类型和项目){
对于(int index=0;indexfriend ostream&operator如果dataArray是该类的成员,那么您看到的可能是在数组边界之外赋值的副作用

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

情景:

循环通过数组,没有找到任何内容。
现在索引已超出范围。
检查该位置的项目是否等于项目。 它可能等于项。
然后你把“最后一个”复制到那个地方数组中的项。

我解决了这个问题,这要归功于人们迫使我在一个文件中创建一个较小的版本。在原始程序中,我有多个对象要操作,因此我在main.cpp中创建了一个函数,该函数将处理函数paramater中提供的不同对象。这样做就是传递一个副本将对象转换为helper函数,而不是引用。插入函数工作正常的原因是我作为引用传递了。感谢您的帮助,如果我从这次经历中学到了什么,那就是我应该提供最小化的完全可编译的代码,这在将来可能会使我不必寻求帮助。

that没有解释为什么本质上无符号整数不会递减。您是否检查了打印消息的代码?您应该调试它,并使用debugger@YochaiTimmer我已经检查了所有的代码,并在许多地方添加了输出,以跟踪filled中的更改,出于某种原因,只在该函数中添加fi的值未在其外部更改lled。@cmac147您需要使用filled--而不是--filled…。请参阅post和pre增量之间的差异。。