Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't assign iterator with no viable overloaded '=' error

I have a field defined as

const vector<record>* data;

where record is defined as

const unique_ptr<vector<float>> features;
const float label;

In my main code, I use

vector<record>::iterator iter = data->begin()

The compiler isn't happy with my code with the no viable overloaded '=' error at that iterator assignment line. It also produces this warning:

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:1097:7: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from '__wrap_iter<const_pointer>' to 'const __wrap_iter<class MLx::Example *>' for 1st argument

What am I doing wrong?

like image 375
zer0ne Avatar asked Mar 16 '23 18:03

zer0ne


1 Answers

"an iterator should be lightweight and should now own the data, i.e. there should be no attempt to copy or even touch record when I make the assignment."

It has nothing to do with the iterator would own the data stored in data data, but the const unique_ptr<>, restricts accessing the template argument types only as const instances.
This means in turn, you need to use

vector<record>::const_iterator iter = data->begin();
             // ^^^^^^

in your main code.

It's much like writing

const vector<record>* data;

As @Jonathan Potter mentioned in his comment

auto iter = data->begin();

should work as well.

like image 193
πάντα ῥεῖ Avatar answered Apr 01 '23 23:04

πάντα ῥεῖ