简体   繁体   English

为索引分配值时,自定义c ++数组异常

[英]Custom c++ array freaks out when assigning a value to an index

I need help with error in my custom c++ array. 我需要有关自定义c ++数组中的错误的帮助。 I wanted to create a simple array template class, intentionally ignoring the std array. 我想创建一个简单的数组模板类,有意忽略std数组。 But when I try to assign a value via index operator, for some reason the array size seems to change and the value does not get stored. 但是,当我尝试通过索引运算符分配值时,由于某种原因,数组大小似乎发生了变化,并且未存储该值。

This is my array template class. 这是我的数组模板类。

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);
}

This is my catch2 unit test environment, where i want to test if my array works properly. 这是我的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 ||下标> =长度)”更改为“如果(下标<0 ||下标> =长度)”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM