Vector Class C ++的变量输出不正确

时间:2015-11-10 16:44:17

标签: c++ arrays variables vector max

我对临时数组大小调用的输出不能正确输出。它按照相应的大小调整,但我不能让MAX显示新数组的新值。我的错误在类中的Resize函数内。

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

1 个答案:

答案 0 :(得分:0)

您的代码有些问题。第一个,可能不是你最关心的那个,就是你永远不会释放记忆。你应该在你的析构函数中做到这一点,或者更好地使用std::unique_ptr来处理你的记忆。

现在,我相信你自己对自己的变量感到困惑。我看到你拥有一个你从未使用的名为num的变量成员。更糟糕的是,AddItem中的参数名称相同。你确定它能做到你想要的吗? growth也是如此。我会建议您以不同的方式命名您的成员变量,以便您快速了解它们的含义。我在它们前面加上&#34; m _&#34;例如,你可以按照自己的意愿去做。 您不需要在类中声明函数参数。仅在函数原型中。

然后,在您的AddItem函数中,使用变量Size来确定添加新元素的位置,但也使用它初始化数组,这意味着您不仅不添加你的元素在数组的开头,你试着把它们写在你不拥有的内存中!

我可以继续很长一段时间。对不起,但我觉得你根本不懂C ++。你应该再次学习基础知识,也许从一个更简单的项目开始你的C ++学习。

祝你好运: - )