矢量类C++的变量输出不正确

Incorrect Variable output with Vector Class C++

本文关键字:变量 输出 不正确 C++      更新时间:2023-10-16

我对临时数组大小调用的输出无法正确输出。它根据调整大小,但我无法获得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;
}

您的代码出现了一些问题。第一个,可能不是你最关心的,是你永远不会释放内存。您应该在析构函数中执行此操作,或者更好地使用std::unique_ptr来处理内存。

现在,我相信你自己也对自己的变量感到困惑。我看到您拥有一个名为num的变量成员,但从未使用过。更糟糕的是,AddItem中有一个同名的参数。你确定它符合你的要求吗?growth也是如此。我建议您以不同的方式命名您的成员变量,以便您快速了解它们是什么。例如,我在它们前面加上"m_",但您可以随心所欲。您不需要在类中声明函数参数。仅在功能原型中。

然后,在AddItem函数中,您可以使用变量Size来确定在哪里添加新元素,但也可以使用它初始化数组,这意味着您不仅不在数组的开头添加元素,还可以尝试将它们写入您不拥有的内存中!

我可以继续很长一段时间。很抱歉,我觉得你根本不懂C++。你应该重新学习基础知识,也许可以从一个更容易的项目开始学习C++。

祝你好运:-)