2015-11-10 44 views
0

我輸出的臨時數組大小的調用不會正確輸出。它調整爲相應的,但我不能讓MAX顯示新數組的新值。我的錯誤在類中的Resize函數中。矢量類C++的錯誤變量輸出

#include <iostream> 
#include <vector> 
#include <string> 
#include <math.h> 
#include <ctime> 
using namespace std; 


class VectorClass { 



private: 
    int * Vector;//This will be our resizeable array 
    int Size; //Keep track of vector current size 
    int MAX=10; 
    int growth = 5; 
    int num; 
    int Resize(int growth, int MAX); 


public: 

    VectorClass(int growth, int Size); 
    ~VectorClass(); 
    int AddItem(int num); 
    void RemoveItem(); 
    void Print(void); 

}; 

VectorClass::VectorClass(int growth, int Size) 
{ 

    Size = 10; 
    growth = 5; 

    Vector = new int[Size]; 



} 

VectorClass::~VectorClass() 
{ 


    cout << "Destructor was called." << endl; 

} 

//Will insert num into the vector at the current open position 
int VectorClass::AddItem(int num) 

{ 


    Vector[Size] = num; 
    Size++; //Indicate that there isnt as much free space 

    if (Size == MAX) 
    { 
     Resize(Size, MAX); 
    } 
    Print(); 

    return num; 
} 


//Get rid of the most recently added item 
void VectorClass::RemoveItem() 
{ 


    Size--; //Tricks the vector into one fewer elements in it it currently does 
    Print(); 


} 

int VectorClass::Resize(int growth, int MAX) 
{ 
    cout << "Array is full! Resizing the Array!" << endl; 

    //Step 1: make a copy 
    int * temp = new int[MAX]; //Make a new array, same size as exiting array 

           //loop that copies the original into the copy 
    for (int i = 0; i<MAX; i++) 
    { 
     temp[i] = Vector[i]; 
    } 

    //Step 2: Delete the original 
    delete[] Vector; //Deletes all elements in the array Vector from the Heap 

        //Step 3: Make a bigger vector 
    Vector = new int[MAX + growth]; 

    //Step 4: Reverse the copy and record the size change 
    for (int i = 0; i<MAX; i++) 
    { 
     Vector[i] = temp[i]; 
    } 
    MAX = MAX + growth; 

    //Step 5: Delete the copy 
    delete[] temp; 

    cout << "Resize was called.\n" << endl; 

    return MAX; 
} 

void VectorClass::Print() 
{ 
    cout << "*******************************************************" << endl; 

    for (int i = 0; i< Size; i++) 
    { 
     cout << Vector[i] << endl; 
    } 
    cout << "Size = " << Size << "\tMAX = " << MAX << "\t Growth = " << growth << endl << endl; 
    cout << "*******************************************************" << endl; 
} 

int main(void) 
{ 



    VectorClass V(5,10); 


    for (int i = 0; i <= 4; i++) 
    { 
     int x = rand(); 

     V.AddItem(x); 

    } 



    //Print the Vector #1 
    V.Print(); 


    //Delete 2 Items 
    V.RemoveItem(); 
    V.RemoveItem(); 



    //Add 9 random Numbers  
    for (int i = 0; i <= 8; i++) 
    { 
     int x = rand(); 

     V.AddItem(x); 

    } 

    //Print the Vector 
    V.Print(); 







    system("pause"); 
    return 0; 
} 
+0

當您在此處發佈代碼時,請考慮刪除所有連續的空白行。使用所有這些空白行,您會添加不必要的滾動查看代碼,並且我認爲這會影響可讀性(您有5頁代碼!)。 – crashmstr

+0

抱歉。我已經改變了這段代碼幾次 – jkorszun

+0

你的構造函數沒有任何意義。您忽略傳遞的參數,並且您沒有指定初始值就具有初始大小(而不僅僅是初始容量)。 – JSF

回答

0

你的代碼有幾個錯誤。第一個,可能不是你最關心的那個,就是你永遠不會釋放記憶。你應該在你的析構函數中做,或者更好地使用std::unique_ptr來處理你的記憶。

現在,我相信你自己對自己的變量感到困惑。我看到你擁有一個你永遠不會使用的名爲num的可變成員。更糟糕的是,您在AddItem中有一個相同名稱的參數。你確定它做了你想要的嗎? growth也是如此。我建議你以不同的方式命名你的成員變量,以便你快速知道它們是什麼。例如,我用「m_」作爲前綴,但是你可以按照你的意願去做。 你不需要在你的類中聲明你的函數參數。只在函數原型中。

然後,在您的AddItem函數中,您使用變量Size來確定添加新元素的位置,但是也使用它初始化您的數組,這意味着不僅在您的開始時不添加元素數組,你試圖將它們寫入你不擁有的內存中!

我可以繼續很長一段時間。我很抱歉,但在我看來,你根本不瞭解C++。你應該再次學習基礎知識,也許從一個更簡單的項目開始你的C++學習。

祝您好運:-)