我做了自己的std ::矢量,但我正在产生错误

I made my own std::vector but I am generating an error

本文关键字:错误 矢量 自己的 std      更新时间:2023-10-16

我将内存分配给T* data以及我的破坏者是否有问题?请注意,T* data已经被作为私人变量给了我。我一直在终端中遇到细分故障错误。另外,我会收到以下警告:

unused variable 'data_' [-Wunused-variable] T* data_ = new T[size_];

这是我的代码:

public:
    // Define the iterator type to just be a pointer to T.
    typedef T* iterator;
    // Constructs an empty vector. Allocate an initial capacity of 1 element,
    // but do not add an element to the vector (i.e. capacity will be 1 while
    // size will be 0). You do not need to worry about bad_alloc exceptions.
    csc340_vector() {
        size_ = 0;
        capacity_ = 1;
        T* data_ = new T[size_];
    }
    // Destructs/de-allocates dynamic memory (if any was allocated).
    ~csc340_vector(){
       delete [] data_;
    }
private:
    T* data_;                // Storage for the elements
    unsigned int size_;      // Number of elements defined in the vector
    unsigned int capacity_;  // Number of elements that the vector can hold
};

另外,我尝试使用T* data_ = new T[capacity_];,但它仍然会生成相同的未使用的变量警告。

        T* data_ = new T[size_];

不是您的会员数据_。这是本地声明的变量。

使用您写的成员

data_ = new T[size_];

或是否有歧义

this->data_ = new T[size_];