【发布时间】:2020-11-26 07:27:34
【问题描述】:
好的,所以基本上是的,我的作业需要帮助,我什至不会说谎,哈哈^^;所以我们要做的一件事就是让所有这些方法和谐地协同工作,直到我们实验室所在的编译器程序说通过image of complier showing the pass or fail messages
这是编译器的另一张图片,以及它在运行时如何根据结果显示通过或失败
所以我有以下需要帮助的事情,所以默认设置,因为它显示通过,我相信我做对了,还有其他一些事情我仍然需要帮助,是的,我自己一直在努力我只是很好的指针总是把我搞砸了xD提前谢谢。
template<typename Type>
class DynArray {
friend class TestSuite; // Giving access to test code
Type* mArray;
unsigned int mSize;
unsigned int mCapacity;
public:
// Default constructor
// Creates a new object
// In: _startingCap An initial capacity to start the array at (optional)
DynArray(unsigned int _startingCap = 0)
{
mArray = new Array[strlen(_startingCap) + 1];
strcpy_s(mArray, strlen(_startingCap) + 1, _startingCap);
}
// Destructor
// Cleans up all dynamically allocated memory
~DynArray()
{
delete[] mArray;
}
// Copy constructor
// Used to initialize one object to another
// In: _da The object to copy from
DynArray(const DynArray& _da)
{
*this = _da;
}
// Assignment operator
// Used to assign one object to another
// In: _da The object to assign from
//
// Return: The invoking object (by reference)
// This allows us to daisy-chain
DynArray& operator=(const DynArray& _da) {
}
// Overloaded [] operator
// Used to access an element in the internal array (read-only)
// In: _index The index to access at
//
// Return: The item at the specified index (by reference)
const Type& operator[](int _index) const
{
}
// Overloaded [] operator
// Used to access an element in the internal array (writeable)
// In: _index The index to access at
//
// Return: The item at the specified index (by reference)
Type& operator[](int _index) {
}
// Get the current number of elements actively being used
//
// Return: The current number of elements used
int Size() const {
}
// Get the current capacity of the internal array
//
// Return: The capacity of the array
int Capacity() const {
}
// Clear the class for re-use
// Should clean up all dynamic memory and leave the object the same as if the default constructor had been called
void Clear() {
}
// Add an item to the end of the array
// Should resize the array if needed
// In: _data The item to be added
void Append(const Type& _data) {
}
// Resizes the internal array, and copies all data over
// In: _newCapacity The new capacity of the array
// NOTE: If 0 is passed, the array should double in size
// If _newCapacity < mCapacity, do nothing
//
// SPECIAL CASE: If mCapacity is 0, then it should be set to 1
void Reserve(unsigned int _newCapacity = 0)
{
if (_newCapacity < mCapacity)
{
continue;
}
if (mCapacity = 0)
{
mCapacity = 1;
}
}
};
#endif
【问题讨论】:
-
我认为“继续”对于底部的一件事来说非常接近
-
if (mCapacity = 0)您缺少用于比较的等号==。现在它每次都分配0。你也不能在循环之外继续。 -
omg bless
-
哦,已经设置的字段是代码提供给我们的方式,所以这就是为什么有字段但它们是空的
-
根据您提供的代码,在您获得正确的创建和复制语义之前,不要尝试实现任何其他功能。这意味着您实现了构造函数、复制构造函数、析构函数,然后是赋值运算符。然后测试复制 DynArray 以确保您不会出现内存泄漏/崩溃等问题。一旦您有了它,然后只有这样您才尝试进行附加、清除等操作。
标签: c++ pointers visual-c++ memory-management