我做了自己的std ::矢量,但我正在产生错误
I made my own std::vector but I am generating an error
我将内存分配给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_];
相关文章:
- cmath抛出错误C2062、C2059、C2143和C2447.cmath包含在矢量文件中
- 从矢量中删除元素后出现隔离错误
- 如何使用C++初始化向量;脚本化值不是数组、指针或矢量错误
- 导致Segmentation错误的结构中的矢量
- 运行时错误:矢量下标超出范围:正在检查空集
- C1001内部编译器错误是由于矢量初始化(如数组)引起的
- 迭代二维矢量时发生运行时错误
- 运行时错误:矢量 push_back()
- Stroustrup的代码审查-编程原理-Ch-4-问题:3-错误:矢量下标超出范围
- 默认构造函数的分段错误;矢量未正确初始化
- 错误:矢量下标超出范围.1201路
- 矢量插入() 错误:矢量迭代器偏移超出范围
- 尝试加载文件时出现运行时错误:矢量下标超出范围
- 获取错误:矢量迭代器不兼容
- 内存访问错误-矢量调整大小
- 崩溃错误:矢量下标超出范围
- C++代码中出现错误(矢量实现)
- 调试错误 - 矢量下标超出范围 - PCL
- 调试错误:矢量迭代器不可取消引用,矢量下标超出范围
- Opengl 3.3 c++ 运行时错误"矢量下标超出范围"