locked
for each, in not compiling RRS feed

  • Question

  • For whatever reason I can't get "for each, in" to compile.  I'm using the /clr flag but no dice.  It's like the compiler doesn't recognize it.  Even used the sample code in MSDN lib to make sure I wasn't doing anything stupid.  It's failing on both VS2008 and VS2010 on different projects.

        String ^ MyString = gcnew String("abcd");
        for each ( Char c in MyString )
            Console::Write(c);

    Errors:

    error C2143: syntax error : missing 'in' before 'const'
    error C2059: syntax error : 'const'
    error C2601: '<some function afterwards>' : local function definitions are illegal
    fatal error C1004: unexpected end-of-file found

    Tried a some stl containers too but got the same thing.

        std::list<int> foo;
        for each ( int i in foo ) {}
    Thursday, October 7, 2010 12:26 AM

Answers

  • Ugh!!! Found the problem.  Someone declared this in one of the headers:

    #define in  const

    Sorry for the time waster everyone.

     

    • Marked as answer by geeyef Thursday, October 7, 2010 3:34 PM
    Thursday, October 7, 2010 3:34 PM

All replies

  • The following works for me with /clr enabled in VC++ 2010:

    using namespace System;
    
    int main()
    {
    	String^ MyString = L"abcd";
    	for each (wchar_t c in MyString)
    		Console::Write(c);
    	for each (auto c in MyString) // uses automatic type inference, new to VC++ 2010
    		Console::Write(c);
    }

    The following works for me both with and without /clr enabled:

    #include <list>
    #include <iostream>
    
    int main()
    {
    	std::list<int> foo;
    	foo.push_back(42);
    	foo.push_back(3);
    	for each (int i in foo)
    		std::cout << i << '\n';
    	for each (auto i in foo) // uses automatic type inference, new to VC++ 2010
    		std::cout << i << '\n';
    }

    Double-check to verify that you do not have /Za enabled in your project settings.

    • Edited by ildjarn Thursday, October 7, 2010 5:25 PM typos
    Thursday, October 7, 2010 12:43 AM
  • Ugh!!! Found the problem.  Someone declared this in one of the headers:

    #define in  const

    Sorry for the time waster everyone.

     

    • Marked as answer by geeyef Thursday, October 7, 2010 3:34 PM
    Thursday, October 7, 2010 3:34 PM