矢量迭代器不可解引用-C++vs2010
vector iterator not dereferencable - C++ vs2010
我有这个问题,不知道该怎么办。有人能帮帮我吗?
void ReadFile(vector<ATTR> &attrVect,MyMatrixDataType &trainMatrix, MyMatrixDataType &testSet)
{
fstream file_op("gilad.txt",ios::in);
string sLine = "", first="" ;
vector<int>::iterator ptr ;
int i ;
vector<string>::iterator ptrSV ;
getline(file_op,sLine) ;
// building vector attrVect with all the attribute names and values
while (sLine != "<END_ATTR>")
{
ATTR Temp ;
Temp.attrName=split(Temp.values,sLine,1) ;
if (Temp.values.empty())
{
Temp.values.push_back("yes");
Temp.values.push_back("no");
}
attrVect.push_back(Temp) ;
getline(file_op,sLine) ;
}
ptrSV=attrVect.back().values.begin() ;
for (; ptrSV < attrVect.back().values.end() ; ptrSV++)
*ptrSV=" " + *ptrSV ;
// building The Train Set from File
vector<int> *tempMat = new vector<int>[attrVect.size()] ;
getline(file_op,sLine) ;
while (sLine != "<END_TRAIN>")
{
vector <string> strVect ;
split(strVect,sLine,1) ;
for (ptrSV = strVect.begin(), i=0 ; ptrSV < strVect.end() ; ptrSV ++, i++)
tempMat[i].push_back(attrVect[i].find(*ptrSV)) ;
getline(file_op,sLine) ;
}
// trasnfering the Train set into a 0,1,2 Matrix representation for easier handling
int xSize=attrVect.size(),counter=tempMat[0].size(),j ;
MyMatrixDataType::size_type rows = xSize ;
MyMatrixDataType::size_type cols = counter ;
MyMatrixDataType a(rows, vector<int>(cols,-2));
for (i=0 ; i < xSize ; i++)
for (j=0, ptr=tempMat[i].begin() ; j < counter ; j++, ptr++)
***a[i][j]=*ptr ;***
trainMatrix=a ;
delete []tempMat ;
// building The test Set from File
tempMat = new vector<int>[attrVect.size()] ;
getline(file_op,sLine) ;
while (!sLine.empty())
{
vector <string> strVect ;
split(strVect,sLine,1) ;
for (ptrSV = strVect.begin(), i=0 ; ptrSV < strVect.end() ; ptrSV ++, i++)
tempMat[i].push_back(attrVect[i].find(*ptrSV)) ;
getline(file_op,sLine) ;
}
xSize=attrVect.size() ;
counter=tempMat[0].size();
rows = xSize ;
cols = counter ;
MyMatrixDataType b(cols,vector<int>(rows,-2));
for (i=0 ; i < xSize ; i++)
for (j=0, ptr=tempMat[i].begin() ; j < counter ; j++, ptr++)
b[j][i]=*ptr ;
testSet=b ;
delete []tempMat ;
file_op.close() ;
}
没有测试这是否是唯一的问题,但您必须确保迭代器不等于末尾:
ptrSV != strVect.end()
你做错了:
ptrSV < strVect.end()
这将尝试取消引用两者(ptrSV
和end()
),然后运行元素类的operator <
,这将失败,因为end()
不是有效的引用(本质上就像试图访问空指针)。
编辑:这部分似乎也错了(分号后面的星号是怎么回事?):
for (i=0 ; i < xSize ; i++)
for (j=0, ptr=tempMat[i].begin() ; j < counter ; j++, ptr++)
***a[i][j]=*ptr ; ***
trainMatrix=a ;
"vector迭代器不可取消引用"的意思是"您试图取消引用一个无效的迭代器。"至于是哪一行导致了它,我们需要知道它发生的行。你能指出吗?
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 强制转换为引用类型
- 引用一个已擦除类型(void*)的指针
- 使用变量参数列表为传递的引用赋值(VS2010中出错)
- VS2010:临时不能绑定到非常量引用
- 一个VS2010错误?允许将非常量引用绑定到右值,甚至没有警告
- 矢量迭代器不可解引用-C++vs2010