在两个层迭代器中超载 产生奇怪的错误
overloading ++ in two layer iterator produces strange bug
我将以下C 代码编写为迭代对象向量的向量。我想迭代向量向量中的每个对象。下面的代码有效,但我不明白它具有一个特殊性。
行" int type_size = types-> size();"行是Iterator.hpp中使用的黑客。我真的不太了解该语言,所以我不知道我是否找到了编译器错误,还是我的代码中的错误。不需要变量" type_size"。它用于两行
第一行:
while(s<types_size && (s<0 || (*types)[s]->size()==0 || object==&( ((*types)[s])->end()))){
第二行:
if(s<types_size){
如果" type_size"被第一行中的" type-> size()"替换,则代码在运行时会出现故障。进行相同的替换,但仅在第二行中不会导致SEG故障。我不明白发生了什么。对本代码其余部分的任何评论将不胜感激。
#ifndef _iterator_hpp_
#define _iterator_hpp_
#include <vector>
using namespace std;
typedef double Object;
typedef vector<Object> ObjectVector;
class Region{
public:
vector<ObjectVector*> types;
};
class ObjectIterator{
private:
int s;
vector<ObjectVector*> *types;
protected:
public:
Object *object;
bool finished;
ObjectIterator operator++(){
if (s>=0) object++; // increments to next object
int types_size=types->size(); // this is a hack that fixes a seg fault (compiler bug??)
// *types is a vector<ObjectVector*>. (*types)[s] is the "sth" ObjectVector*.
// &(*iterator) gives a pointer to the object pointed to by iterator.
//((*types)[s])->end()) is an iterator that points past the end of
// the ObjectVector* ((*types)[s])->end())
while(s<types_size && (s<0 || (*types)[s]->size()==0 || object==&(* ((*types)[s])->end()))){
//need to increment to next non-empty types
s++;
if(s<types_size){
object=&((*(*types)[s])[0]);
}else{finished=true;}
}
return (*this);
}
/*---------------------constructor--------------------------------------------------------
start with s=-1 and increment to first object */
ObjectIterator(vector<ObjectVector*> *typesarg):finished(false) {
types=typesarg;s=-1;++(*this);
};
};
#endif
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// it.cpp
// g++ it.pp
#include <iostream>
#include <vector>
#include "iterator.hpp"
using namespace std;
int num_types=3;
int main(){
Region region;
int num_objects[num_types];
num_objects[0]=1;
num_objects[1]=3;
num_objects[2]=5;
// create an ObjectList for each type
for(int s=0;s<num_types;s++){
ObjectVector *objectlist = new ObjectVector;
for(int i=0;i<num_objects[s];i++){
objectlist->push_back((double)2*(i+1)*(s+1));
}
region.types.push_back(objectlist);
}
cout <<"types.size="<< region.types.size()<<endl;
for(ObjectIterator OI(®ion.types); !OI.finished ; ++OI)
{
cout <<*(OI.object)<<endl;
}
}
size()
返回一个未签名的整数,因此:
while(s<types->size()
如果s
为-1(其初始值),则行为不会相同。-1变成一个大型无符号整数,比较是错误的。有关更多信息,请参见签名/未签名的比较。
相关文章:
- 警告处理为错误这里有什么问题
- 为什么我的超载"+"运算符返回的总额错误?
- C bool表达式作为函数参数调用错误的超载
- 错误:没有可行的超载=
- 错误:找不到匹配的超载函数
- C 错误 - 函数不能超载
- 超载 *运算符时无法转换错误
- 此代码运算符超载中的错误是什么?
- 触发编译时间错误时,constexpr函数的超载被调用
- 在两个层迭代器中超载 产生奇怪的错误
- C 流插入超载错误
- 学习OOP:超载 OR =操作员错误
- 为什么我无法使错误操作员()无法超载
- 运算符在C 中超载(小错误)
- &lt;&lt;操作员在C 中超载错误
- 超载功能中的错误
- 超载流提取操作员错误
- 在尝试超载&lt;&lt;时链接错误2005和1169操作员
- 超载功能错误消息
- 超载"+"操作员,结果错误?