我需要自定义c ++数组中的错误帮助。我想创建一个简单的数组模板类,故意忽略std数组。但是当我尝试通过索引运算符分配值时,由于某种原因,数组大小似乎发生了变化,并且值不会被存储。
这是我的数组模板类。
template<typename T>
class Array
{
friend std::ostream &operator<<(std::ostream& output,const Array& a)
{
//output private ptr-based array
for (size_t i=0; i < a.length; i++)
{
output << std::setw(12) << a.ptr[i];
if ((i+1)%4 == 0) //four numbers per row of output
output << std::endl;
}
if (a.length%4 != 0) //end last line of output
output << std::endl;
return output;
}
public:
Array(const int& arraySize = 0);
Array(const Array&);
~Array();
Int32 size();
bool operator==(const Array&) const;
bool operator!=(const Array&) const;
T &operator[](Int32);
const T operator[](Int32)const;
private:
size_t length;
T* ptr;
};
template<typename T>
Array<T>::Array(const int& length):length(length > 0 ? length : throw std::invalid_argument("Array size must be greater than 0")), ptr(new T[length])
{
for(size_t i = 0; i < length; ++i)
{
ptr[i] = NULL;
}
}
template<typename T>
Array<T>::Array(const Array<T>& aryToCpy): length(aryToCpy.size()), ptr(new T[length])
{
for(size_t i = 0; i < length; ++i)
{
ptr[i] = aryToCpy[i];
}
}
template<typename T>
Array<T>::~Array<T>()
{
delete[] ptr;
ptr = nullptr;
}
template<typename T>
Int32 Array<T>::size()
{
return length;
}
template<typename T>
T& Array<T>::operator[](int subscript)
{
if (subscript < 0 || subscript >=0)
throw std::out_of_range("Subscript out of range");
return ptr[subscript];
}
template<typename T>
const T Array<T>::operator[](int subscript) const
{
if (subscript < 0 || subscript >=0)
throw std::out_of_range("Subscript out of range");
return ptr[subscript];
}
template<typename T>
bool Array<T>::operator==(const Array<T>& right) const
{
if(length != right.length)
return false;
for(size_t i = 0; i < length; i++)
{
if(ptr[i] != right.ptr[i])
return false;
}
return true;
}
template<typename T>
bool Array<T>::operator!=(const Array<T>& right) const
{
return !(this == right);
}
这是我的catch2单元测试环境,我想测试我的数组是否正常工作。
SCENARIO("Arrays can be created empty.")
{
GIVEN("An empty Array declaration and a defined length")
{
Array<Int16> *testArray;
Int8 arrayLength = 4;
WHEN("A new array gets created with empty items...")
{
testArray = new Array<Int16>(arrayLength);
THEN("The size of the array is as defined")
{
REQUIRE(testArray->size() == 4);
}
}
testArray = new Array<Int16>(arrayLength);
WHEN("An empty array with size 4 gets filled up with values")
{
std::cout << "Array size start: " << testArray->size() << std::endl;
std::cout << "Array index 0 (before): " << testArray[0] << std::endl;
testArray[0] = 1;
std::cout << "Array index 0 (after): " << testArray[0] << std::endl;
testArray[1] = 2;
testArray[2] = 3;
testArray[3] = 4;
THEN("The array is now filled with values")
{
std::cout << "Array size test: " << testArray->size() << std::endl;
REQUIRE(testArray[0] == 1);
REQUIRE(testArray[1] == 2);
REQUIRE(testArray[2] == 3);
REQUIRE(testArray[3] == 4);
}
}
}
答案 0 :(得分:-1)
尝试改变&#34; if(下标&lt; 0 ||下标&gt; = 0)&#34; to&#34; if(下标&lt; 0 ||下标&gt; = length)&#34;