-
January 25th, 2014, 06:54 AM
#1
foreach loop trouble
Hi !
I would like to know whether foreach instruction is full compatible with VS2012 ?
Thanks
-
January 25th, 2014, 07:20 AM
#2
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.
-
January 25th, 2014, 12:13 PM
#3
Re: foreach loop trouble
Originally Posted by mulfycrowh
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|