How to configure a vector iterator to return a struct?

As part of my current task of converting a C ++ project from Unix to Linux, I now have the following error:

jh205.C: In the member function 'FVSearchLogical_t * FVLogical :: getFirst (long int): jh205.C: 9615: error: invalid cast from type' __gnu_cxx :: __ normal_iterator → to enter 'FVSearchLogical_t * jh205.C: In the function member 'FVSearchLogical_t * FVLogical :: getNext (long int): jh205.C: 9630: Error: cannot convert' __gnu_cxx :: __ normal_iterator → to 'FVSearchLogical_t * in reverse jh205.C: In member function' void FVLogical :: updateTable (): jh205.C: 9656: error: invalid cast type '__gnu_cxx :: __ normal_iterator → for input' 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 structure in question:

 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 a minimal 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(); } 
+1
source share
1 answer

You are trying to return an iterator in the same way that a warning says that you are on a line:

 return m_searchLogical_it; 

To get a raw pointer to an element, which is the return type of getFirst , you need to get a pointer to the m_searchLogical_it object. To do this, you need to dereference the iterator to get the object, then take the address of the object:

 return &*m_serchLogical_it; 

If I can further suggest; you use an iterator ( m_searchLogical_it ) and a loop counter ( i ) when all you need to use is an 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; } } 
+3
source

Source: https://habr.com/ru/post/1014417/


All Articles