CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2013
    Posts
    44

    foreach loop trouble

    Hi !


    I would like to know whether foreach instruction is full compatible with VS2012 ?

    Thanks

  2. #2
    Join Date
    Jan 2013
    Posts
    44

    Re: foreach loop trouble

    Here is more information :

    QList<Line*> _separator;
    foreach (Line* l, l._separator)
    ...

    The line foreach gives a compilation error :
    C2228 : left of _separator must have class/struct/union/generic type

    Line is a declared class.

    Any idea ?
    Many thanks for help.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: foreach loop trouble

    Quote Originally Posted by mulfycrowh View Post
    Here is more information :

    QList<Line*> _separator;
    foreach (Line* l, l._separator)
    ...

    The line foreach gives a compilation error :
    C2228 : left of _separator must have class/struct/union/generic type
    You declared 'l' to be a pointer to a Line. That means you cannot write 'l.'. Pointers don't have members. The second argument of the foreach macro in Qt is the container you want to iterate over. I guess that would be _separator in your code.

    That said, I would advice against using the foreach macro from the Qt framework. It's an awful monstrosity, because it makes a copy of the container. That means you cannot use it when you want to change the values in the container and, otherwise, it still has an overhead. Instead, you can use BOOST_FOREACH as a drop-in replacement without these drawbacks or, if you upgrade to VS 2013, use range-based for loops.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured