矢量类C++的变量输出不正确
Incorrect Variable output with Vector Class C++
我对临时数组大小调用的输出无法正确输出。它根据调整大小,但我无法获得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++。
祝你好运:-)
相关文章:
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 使用静态变量的递归调用的不同输出
- C++具有模板成员变量的类. 和参数内存输出
- 更改输出的变量
- 如何在C++中访问作用域的变量输出?
- 简化C++包括变量名称和函数输出的模式
- C++ - 输出与初始化变量不同?
- 是否可以在C++中基于程序集输出(.dll或.exe)定义变量
- 当对字符变量使用toupper()时,所述char变量输出多个字符.我该如何防止这种情况发生
- 如何让变量随机输出四个单词之一
- 输出(用于 GraphViz)提升图顶点及其属性,使用具有私有变量的类作为捆绑属性
- 如何将C++输出捕获到 Bash 变量?
- 当我在循环内打印变量时无限输出
- 字符串比较在与变量一起使用时给出不同的输出
- 双变量输出 32767 始终代替用户输入
- 使用std :: ofstream中的c 中的float变量输出
- C++:Xcode中未初始化的变量输出为0
- C++转义字符和引用变量输出混淆
- 矢量类C++的变量输出不正确
- 硬编码变量输出中没有反斜杠,但在文件输出中存在