I cannot use =operator in the below code, as I am getting compiller error. I cannot understand what is wrong.
int CProcessData::calculateMidPoints(const std::vector<double>& xv, const std::vector<double>& yv)
{
if((0 == xv.size()) || (0 == yv.size()))
return 1;
std::vector<double>::iterator it;
for (it = xv.begin(); it < xv.end(); it++)
{
}
return 0;
}
I am getting following error:
../src/CProcessData.cpp: In member function ‘int CProcessData::calculateMidPoints(const std::vector<double>&, const std::vector<double>&)’:
../src/CProcessData.cpp:44:9: error: no match for ‘operator=’ (operand types are ‘std::vector<double>::iterator {aka __gnu_cxx::__normal_iterator<double*, std::vector<double> >}’ and ‘__gnu_cxx::__normal_iterator<const double*, std::vector<double> >’)
I would aprichiate all help!
xv
is a const
reference, meaning only const
member functions can be called in it. The const
overload of std::vector<double>::begin()
returns a const_iterator
, and that cannot be used to construct an iterator
because it would break const-coreectness.
So you need
std::vector<double>::const_iterator it;
Note that since C++11 you have other alternatives:
for (auto it = xv.begin(); it < xv.end(); it++)
or, if you're iterating over all elements, a range-based loop might be better:
for (auto x: xv) { ... // x is a copy
for (auto& x: xv) { ... // x is a reference
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.