0

我需要帮助解决我的自定义 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);
        }
    }
}
4

1 回答 1

-1

尝试将“if (subscript < 0 || subscript >=0)”更改为“if (subscript < 0 || subscript >= length)”

于 2018-06-05T16:10:41.127 回答