C++ 创建对象的动态数组?
Posted
技术标签:
【中文标题】C++ 创建对象的动态数组?【英文标题】:C++ creating dynamic array of objects? 【发布时间】:2014-10-22 23:34:48 【问题描述】:#include "resistor.h"
void main()
srand(GetTickCount()); // Lewt's start by seeding the random number generator so that different runs of our program will always yield fifferent results
Resistor * pResistor; // Pointer to a resistor class
int numberOfResistors = 0; // Variable will hold the user choice of the number of resistors to be created
cout << "How many resistors do you want to create?" << endl; // prompt user
cin >> numberOfResistors; // Allow the user to enter the number of resistors to be created
pResistor = new Resistor[numberOfResistors]; // Create an array of objects of class resistor and assign its address to our pointer
// You will notice that there is a logic error here. All the resistors will have the same value. This
// is because they were all created at once. To address this issue we need to create each object
// of the class resistor separately. This means using a loop. This will be up to you to do
// It should be a very easy fix
for (int i = 0; i< numberOfResistors; i++) // This loop will be used to display the resistor values
cout << "Resistor # " << i + 1 << " has a value of " << pResistor->getResistance() << " " << (char)234 << endl; // Display value of resistor pointed to by pResistor
cout << "So far, we have created " << pResistor->getNumerOfResistors() << " resistor(s)" << endl; // Display total number of resistors
delete[] pResistor; // Delete the array that was created and release the memory that was allocated.
// end of main
我正在使用此代码进行课堂作业。正如您可能在 cmets 中看到的那样,我的教练在那里输入了一个逻辑错误,我需要修复它。我尝试将 pResistor = new Resistor[numberOfResistors];
行放在一个 for 循环中,该循环运行用户输入指定的次数(而不是创建一个数组)。问题仍然是每个对象仍然使用相同的值创建 ResistorObject.resistance
任何意见将不胜感激
编辑:下面的代码是我上面试图解释的:
for (int i = 0; i < numberOfResistors; i++)
pResistor = new Resistor[i]; // Create an array of objects of class resistor and assign its address to our pointer
然而,当我收到错误时,这将无法编译:
错误 C4703:使用了可能未初始化的局部指针变量“pResistor”
【问题讨论】:
如何设置阻力?它是否在用户定义的(非默认)构造函数中指定?你应该使用 RNG 这样做吗? 你不需要注释每一行代码——我们知道 C++。过度注释实际上会使您的代码更难阅读。 向量是显而易见的方法,例如 user3670482 的答案显示。该向量包含许多指向不同电阻器的单独指针,每个电阻器都被分配,因此可以不同地设置它们的电阻,但是你又如何设置电阻?另外,你应该在没有向量的情况下这样做吗? 将pResistor->getResistance()
更改为 pResistor[i].getRsistance()
。不要更改您的new
您的编辑是内存泄漏中心:它不断尝试将不同数量的内存分配给同一个指针变量,而您却无法再次释放它。您的代码正在尝试为 pResistor 分配 0 个电阻,然后是 1,然后是 2,然后是 3……等等。
【参考方案1】:
srand(GetTickCount());
Resistor * pResistor = NULL;
vector<Resistor*> resList;
int numberOfResistors = 50; // Use your cin cout here
for (size_t i = 0; i < numberOfResistors; i++)
pResistor = new Resistor;
// Assuming some sort of pResistor->SetResistance( /* val */ )
resList.push_back(pResistor);
for (int i = 0; i< numberOfResistors; i++) // This loop will be used to display the resistor values
cout << "Resistor # " << i + 1 << " has a value of " << resList[i]->getResistance() << " " << (char)234 << endl; // Display value of resistor pointed to by pResistor
cout << "number of resistors : " << resList.size() << endl;
每个电阻都需要自己的参考,所有参考都必须添加到有序列表中。 std::vector 用于制作有序列表
【讨论】:
以上是关于C++ 创建对象的动态数组?的主要内容,如果未能解决你的问题,请参考以下文章