Reputation: 14869
I am trying to use comma to separate multiple initialization within a for loop, but I am getting the error below.
Is is legal to use the comma in the first part of a for-loop?
error: too few template-parameter-lists
error: sEnd was not declared in this scope
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
using namespace std;
typedef vector<int> Vc;
Vc v;
for(Vc::iterator sIt = v.begin(), Vc::iterator sEnd = v.end();
sIt != sEnd; ++sIt) {
// do something
}
return 0;
}
Upvotes: 3
Views: 1661
Reputation: 3225
Should just be:
/* remove this */
for(Vc::iterator sIt = v.begin(), /* Vc::iterator */ sEnd = v.end();
sIt != sEnd; ++sIt) {
// do something
}
Becomes:
for(Vc::iterator sIt = v.begin(), sEnd = v.end();
sIt != sEnd; ++sIt) {
// do something
}
Also, this is not a usage of the comma operator (the comma operator can only be used in an expression); this is a simple variable declaration.
Upvotes: 8
Reputation: 490398
Using a comma is allowed, but trying to start what follows the comma with a typename is not.
Upvotes: 1