在定义模板类、操作符和迭代器时遇到麻烦
trouble defining template classes, operators, and iterators
所以我试图定义一个模板类"TwoWayVector"answers"TwoWayVectorIterator",但我遇到了很多问题。我想在TwoWayVector中定义==来返回一个引用,并将const TwoWayVector作为参数,这就是我定义其他操作符的方式,g++也没有抱怨,但由于某种原因,下面的代码产生了错误
TwoWayVector.cc: In member function ‘bool& TwoWayVector<T>::operator==(TwoWayVector<T>) [with T = int]’:
Test.cc:10: instantiated from here
TwoWayVector.cc:40: error: passing ‘const TwoWayVector<int>’ as ‘this’ argument of ‘T& TwoWayVector<T>::operator[](int) [with T = int]’ discards qualifiers
Test.cc:10: instantiated from here
TwoWayVector.cc:32: warning: reference to local variable ‘result’ returned
Test.cc
#include <iostream>
#include "TwoWayVector.cc"
int main(){
TwoWayVector<int> numbers;
TwoWayVector<int> numbers2;
numbers.push_back(3);
numbers.push_back(2);
numbers2.push_back(3);
numbers2.push_back(2);
cout << (numbers==numbers2);
cout << endl;
return 0;
}
TwoWayVector.cc
using namespace std;
#include "TwoWayVectorIterator.cc"
template <class T> class TwoWayVector{
public:
T* data;
int capacity;
int nextFree;
TwoWayVector(){
capacity = 10;
nextFree = 0;
data = new T[capacity];
}
~TwoWayVector(){
delete data;
}
T& operator[](const int index){
if( index >= capacity || capacity + index < 0){
string number = static_cast<ostringstream*>( &(ostringstream() << index) )->str();
string error = "index " + number + " is out of bounds";
throw error;
}
else if(index < 0){
return data[nextFree+index];
}
return data[index];
}
bool& operator==(const TwoWayVector vector2){
bool result = true;
if(capacity != vector2.capacity){
result = false;
}
if(nextFree != vector2.nextFree){
result = false;
}
for(int i=0; i<nextFree ; i++){
if(data[i] != vector2[i]){
result = false;
}
}
return result;
}
//memory leaks?
void push_back(T object){
if(capacity <= nextFree){
capacity = capacity*2;
T* tmp = new T[capacity];
for(int i=0; i<capacity; i++){
tmp[i] = data[i];
}
delete data;
data = tmp;
}
data[nextFree] = object;
nextFree++;
}
T pop_back(){
nextFree--;
T result = data[nextFree];
data[nextFree] = NULL;
return result;
}
int size(){
return nextFree;
}
TwoWayVectorIterator<T>* begin(){
TwoWayVectorIterator<T>* i = new TwoWayVectorIterator<T>(0,this);
return (i);
}
TwoWayVectorIterator<T>* end(){
TwoWayVectorIterator<T>* i = new TwoWayVectorIterator<T>(nextFree,this);
return(i);
}
};
TwoWayVectorIterator.cc
#include <sstream>
using namespace std;
template<typename T> class TwoWayVector;
template <class T> class TwoWayVectorIterator{
public:
TwoWayVector<T>* vector;
int currentPosition;
TwoWayVectorIterator(TwoWayVector<T>& vec){
currentPosition = 0;
vector = vec;
}
TwoWayVectorIterator( int pos , TwoWayVector<T>* vec){
currentPosition = pos;
vector = vec;
}
bool& operator==(const TwoWayVectorIterator vector2){
bool contents, position;
contents = (vector == vector2) ? true : false;
position =(currentPosition == vector2->currentPosition) ? true : false;
return (contents && position);
}
bool& operator!=(const TwoWayVectorIterator vector2){
bool contents, position;
contents = (vector == vector2) ? false : true;
position=(currentPosition == vector2->currentPosition) ? false : true;
return (contents || position);
}
TwoWayVectorIterator& operator++(){
return *this;
currentPosition = (currentPosition+1);
}
TwoWayVectorIterator& operator++(int){
currentPosition = (currentPosition+1);
return *this;
}
TwoWayVectorIterator& operator=(TwoWayVectorIterator* vector2){
&vector = vector2;
currentPosition = vector2->currentPosition;
return *this;
}
TwoWayVectorIterator& operator+(int n){
currentPosition = currentPosition+n;
return *this;
}
TwoWayVectorIterator& operator-(int n){
currentPosition = currentPosition-n;
return *this;
}
bool& operator<(TwoWayVectorIterator* vector2){
return (currentPosition<vector2->currentPosition);
}
T& operator*(){
return vector[currentPosition];
}
};
如果我改变TwoWayVector中的==运算符定义。抄送至
bool operator==(TwoWayVector vector2){
bool result = true;
if(capacity != vector2.capacity){
result = false;
}
if(nextFree != vector2.nextFree){
result = false;
}
for(int i=0; i<nextFree ; i++){
if(data[i] != vector2[i]){
result = false;
}
}
return result;
}
然后一切都编译了,但是当我运行它时,我得到
1
a.out(40908) malloc: *** error for object 0x7fe4f2c03b40: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
任何想法?
在operator==
内部调用operator[]
,它是非const的,在const变量vector2
上。
你应该添加一个操作符的只读版本:
const T & operator[](const int index) const;
但是你应该像这样使用object形参的引用:
bool operator==(const TwoWayVector &vector2) const;
否则,const
关键字并没有真正做很多事情,因为它只是说对象的副本,它只是在函数的堆栈上创建的,不能修改,这并不重要。(因此,解决这个问题的最简单方法是从vector2
中删除const关键字,但这并不完全正确。)
当然,不要返回bool值作为引用,因为它引用了变量result
,一旦您离开函数,该变量将不再存在。
基本上由于缺乏复制构造函数而发生崩溃。参见三法则。当
bool operator==(TwoWayVector vector2){
在main中使用,创建vector的副本,保存numbers2的指针,然后在退出操作符==时删除副本,使numbers2具有指向已释放内存的指针,在退出main()时试图删除这些指针,这会导致错误。
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 如何在c++迭代器类型中包装std::chrono
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 对于set上的循环-获取next元素迭代器
- 为什么output_editor Concept不需要output_e迭代器标记
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 使用迭代器时如何访问对象在向量中的位置?
- std::vector::迭代器是否可以合法地作为指针
- 跟随整数索引列表的自定义类迭代器
- 不明白迭代器,引用和指针失效,一个例子
- 我可以使用反向迭代器作为ForwardIt吗
- ESP8266单片机矢量迭代器的C++问题
- 使用迭代器时遇到问题,每 M 次传递一次擦除一个向量项
- 尝试设置迭代器时会遇到奇怪的错误
- 在c++中使用列表迭代器时遇到问题
- 删除元素时使用 STL 映射的迭代器时遇到问题
- 在定义模板类、操作符和迭代器时遇到麻烦