如何调整vector迭代器以返回struct? [英] How to adjust vector iterator to return struct?
问题描述
作为我将C ++项目从Unix转换为Linux的持续任务的一部分,我现在遇到以下错误:
As a part of my ongoing task of converting a C++ project from Unix to Linux, I now have the following error:
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:错误:无效的强制转换来自键入
'__ gn_cxx :: __ normal_iterator >>'
键入'void *'
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*’
它来自于此代码:
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;
}
它的结构:
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?
添加信息:
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;
获取指向元素的原始指针,返回类型为 getFirst
,你需要获得指向对象 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. 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;
如果我可以另外建议;你正在使用迭代器( m_searchLogical_it
)和一个循环计数器( i
),当你需要使用的只是迭代器:
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:
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;
}
}
这篇关于如何调整vector迭代器以返回struct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!