On the 2nd for-loop, I get the following error from gcc:
error: expected unqualified-id before 'int'
I'm not sure what I'm missing. I've looked over documentation for how a for-loop should look and I'm still confused. What's wrong here?
#include <iostream>
#include <vector>
int main() {
std::vector<int> values;
for (int i = 0; i < 20; i++) {
values.push_back(i);
}
std::cout << "Reading values from 'std::vector values'" << std::endl;
for (int i = 0, int col = 0; i < values.size(); i++, col++) {
if (col > 10) { std::cout << std::endl; col == 0; }
std::endl << values[i] << ' ';
}
}
This is akin to a regular multiple variables declaration/initialization in one line using a comma operator. You can do this:
int a = 1, b = 2;
declaring 2 ints. But not this:
int a = 1, int b = 2; //ERROR
Try without the int
before col
.
for (int i = 0, col = 0; i < values.size(); i++, col++)
Others have already told you how to fix the problem you've noticed. On a rather different note, in this:
if (col > 10) { std::cout << std::endl; col == 0; }
It seems nearly certain that the last statement here: col==0;
is really intended to be col=0;
.
This should fix it
for (int i = 0, col = 0; i < values.size(); i++, col++) {
if (col > 10) { std::cout << std::endl; col == 0; }
std::endl << values[i] << ' ';
}
}
A variable definition goes like this
datatype variable_name[=init_value][,variable_name[=init_value]]*;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With