[英]How to adjust vector iterator to return struct?
As a part of my ongoing task of converting a C++ project from Unix to Linux, I now have the following error: 作为我将C ++项目从Unix转换为Linux的持续任务的一部分,我现在有以下错误:
jh205.C: In member function 'FVSearchLogical_t* FVLogical::getFirst(long int)': jh205.C:9615: error: invalid cast from type '__gnu_cxx::__normal_iterator > >' to type 'FVSearchLogical_t*' jh205.C: In member function 'FVSearchLogical_t* FVLogical::getNext(long int)': jh205.C:9630: error: cannot convert '__gnu_cxx::__normal_iterator > >' to 'FVSearchLogical_t*' in return jh205.C: In member function 'void FVLogical::updateTable()': jh205.C:9656: error: invalid cast from type '__gnu_cxx::__normal_iterator > >' to type 'void*'
jh205.C:在成员函数'FVSearchLogical_t * FVLogical :: getFirst(long int)'中:jh205.C:9615:错误:类型'__gnu_cxx :: __ normal_iterator>>'无效转换为'FVSearchLogical_t *'jh205.C:在成员函数'FVSearchLogical_t * FVLogical :: getNext(long int)':jh205.C:9630:错误:无法将'__gnu_cxx :: __ normal_iterator >>'转换为'FVSearchLogical_t *'作为回报jh205.C:在成员函数'void FVLogical :: updateTable()':jh205.C:9656:错误:从类型'__gnu_cxx :: __ normal_iterator>>'转换为无效,以输入'void *'
It comes from this code: 它来自这段代码:
FVSearchLogical_t * FVLogical::getFirst(long sensorId) {
// loop through the Search vector to find the first one for the sensor
m_searchLogical_it = m_FVSearchVector.begin();
for(int i=0; i < m_FVSearchVector.size(); i++){
// as soon as we find the first one return it
if(m_searchLogical_it->ml_sensorId == sensorId) {
return m_searchLogical_it;
}
m_searchLogical_it++;
}
return NULL;
}
The struct it is about: 它的结构是:
typedef struct {
long ml_sensorId;
char mc_startDate[10];
char mc_startTime[10];
char mc_endDate[10];
char mc_endTime[10];
long ml_startBlk;
long ml_endBlk;
long ml_sendUnit;
} FVSearchLogical_t;
Any suggestions on how to do this with the least amount of code changes in the project? 有关如何使用项目中最少量的代码更改执行此操作的任何建议?
Added information: 补充信息:
FVLogical::~FVLogical(){
m_FVSearchVector.clear();
m_FVInsertVector.clear();
m_rptDataVector.clear();
m_rptHeaderVector.clear();
m_rptFooterVector.clear();
}
You are trying to return an iterator exactly as the warning says you are in the line: 您正在尝试返回一个迭代器,正如警告您说的那样:
return m_searchLogical_it;
To get a raw pointer to the element, which is the return type of getFirst
, you'll need to get a pointer to the object m_searchLogical_it
points to. 要获取指向元素的原始指针(
getFirst
的返回类型),您需要获取指向m_searchLogical_it
指向的对象的指针。 To do that you'll need to dereference the iterator to get the object, then take the address of the object: 要做到这一点,你需要取消引用迭代器来获取对象,然后获取对象的地址:
return &*m_serchLogical_it;
If I may additionally suggest; 如果我可以另外建议; you're using an iterator (
m_searchLogical_it
) and a loop counter ( i
), when all you need to use is the iterator: 当你需要使用的只是迭代器时,你正在使用迭代器(
m_searchLogical_it
) 和循环计数器( i
):
for(m_searchLogical_it = begin(m_FVSearchVector); m_searchLogical_it != end(m_FVSearchVector); ++m_searchLogical_it) {
if(m_searchLogical_it->ml_sensorId == sensorId) {
return &*m_searchLogical_it;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.