6 years, 9 months ago.

Range based for loops fails

How comes my range based for loop fails with the online compiler:

std::vector<uint8_t> data; std::ostringstream oss; for (int n : data) { oss << std::setfill('0') << std::setw(2) << std::uppercase << std::hex << n; }

Error: Expected a ";" in "CaynenneLPP/CayenneLPP.h", Line: 59, Col: 17 Error: Expected an expression in "CaynenneLPP/CayenneLPP.h", Line: 59, Col: 23 Error: Expected a ";" in "CaynenneLPP/CayenneLPP.h", Line: 59, Col: 23

It's c++11, but as I read it the online compiler should be c++1 compatible

http://en.cppreference.com/w/cpp/language/range-for

Regards, Tom

1 Answer

6 years, 9 months ago.

Hi Tom,

Currently, the online compiler does not support C++11, if you want to use range based for loops you will have to compile your project locally using the mbed-cli tools.

Alternatively, you can use iterators

using std::vector;
std::vector<uint8_t> data; 
std::ostringstream oss;

for(vector<uint8_t>::iterator it = data.begin(); it != data.end(); it++){
  oss << std::setfill('0') << std::setw(2) << std::uppercase << std::hex << n;
}

Hope this helps, -Michael

Accepted Answer