Question

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;
}
Was it helpful?

Solution

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.

OTHER TIPS

Using a comma is allowed, but trying to start what follows the comma with a typename is not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top