2013-02-01 47 views
-3

附加的代碼似乎按照需要工作,但是當它位於不同的文件main.cpp,Set.h,Set.cpp中時,不會在remove函數之外遞減。我真的不明白這裏發生了什麼。或者爲什麼它會在一個文件中產生任何影響。C++成員變量沒有在功能之外被改變

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

回答

1

我想出了問題感謝人們迫使我在一個文件中製作一個更小的版本。在原始程序中,我有多個要操作的對象,所以我在main.cpp中創建了一個函數,它可以處理函數參數中提供的不同對象。這樣做是我將對象的副本傳遞給幫助函數而不是引用。插入函數工作正常的原因是因爲我已經通過作爲參考。感謝您的幫助,如果我從這個經歷中學到了什麼,我應該提供最小化的完全可編譯的代碼,這在將來可能會阻止我尋求幫助。

1

如果dataArray中是類的成員,你看到的可能是賦值出數組邊界的一側的副作用是什麼。

會發生什麼是值將被設置到一個相鄰的類成員,你不會得到任何異常,因爲內存確實屬於你的程序(它仍然在類中)。

該場景:

循環遍歷數組,找不到任何東西。
現在索引是超出界限。
您檢查該位置的物品是否等於物品。 它可能等於item。 然後您將數組中的「最後一個」項複製到該位置。

+0

這並不能解釋爲什麼基本上無符號整數不會遞減。 – trumpetlicks

+0

您是否檢查過打印該消息的代碼?你應該調試它並使用調試器自己查看這些值 –

+0

@YochaiTimmer我已經檢查了所有的代碼,並且在很多地方添加了輸出來跟蹤填充的變化,由於某種原因,只有在該函數中填充的值沒有被改變在它之外。 – cmac147