Vulryn
Vulryn

Reputation: 23

this range-based 'for' statement requires a suitable function and none was found

I am new to programming and I am trying to use a range-based for loop in a code i am writing. Loops range is based on 2 input taken by a user.

But my code doesn't compile and it gives error:

"this range-based 'for' statement requires a suitable function and none was found".

Is there anyway to fix this problem or should i use something other than range-based for loop ? I should specify that i am not familiar with pointers nor classes.

Shortened version of my code :

cin >> rangestart>> rangeend;
int val{ rangestart };
for (auto val : rangeend)
{
    vec.push_back(val);
}

Upvotes: 2

Views: 8214

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385108

for (auto val : rangeend)

This does not mean "iterate from val to rangeend".

It means "iterate over the collection rangeend, with each element being re-declared as auto val".

The two things are completely different.

Just use a normal loop with your integer.

Upvotes: 3

Bathsheba
Bathsheba

Reputation: 234645

The obvious way is to use for (int val = rangestart; val <= rangeend; ++val).

The range-based for loop is used to iterate over collections. As such, it's inappropriate for your particular use case.

Upvotes: 7

Related Questions