Why won't this compile and how can it be implemented so that it does?

Posted by George Edison on Stack Overflow See other posts from Stack Overflow or by George Edison
Published on 2010-04-21T23:43:04Z Indexed on 2010/04/21 23:53 UTC
Read the original article Hit count: 232

Filed under:
|

Here is some C++ code I'm playing around with:

#include <iostream>
#include <vector>

#define IN ,
#define FOREACH(x,y) for(unsigned int i=0;i<y.size();i++) { x=y[i];
#define ENDFOREACH }

using namespace std;

int main()
{
    vector<int> ints;
    ints.push_back(3);
    ints.push_back(4);
    ints.push_back(5);
    ints.push_back(6);

    FOREACH(int item IN ints)
        cout << item;
    ENDFOREACH

    return 0;
}

However, I get an error:

macro "FOREACH" requires 2 arguments, but only 1 given

The code compiles if I change the IN to a comma. How can I get the IN to take the place of a comma?

© Stack Overflow or respective owner

Related posts about c++

Related posts about macro