Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
My actual programming workbench is MS Visual Studio C++ 2010 SP1Rel.
 
I want to code something like:
 
void print_vector(vector <int>& vec)
{
    for (auto &elem : vec)
        cout << elem << " ";
}
 
but MS says no.
 
Is it really necessary to upgrade to VS 2012 or is there another way?
 
Thanks in advance.
Posted 5-Dec-12 0:30am
Comments
Richard MacCutchan at 5-Dec-12 6:45am
   
but MS says no.
 
What exactly do they say, and where do they say it?
Member 9586495 at 5-Dec-12 7:11am
   
Thanks for your response.
 
I get compiler errors in line
for (auto &elem : vec)

C2143: Syntax error: There must be an ',' before ':'
C2530: 'elem': References must be initialized
C3531: "elem": A symbol, which contains "auto" must have an initializer.
 
(translated from German by me)
Albert Holguin at 5-Dec-12 20:55pm
   
By the way, if you want to respond to someone's post use the "Reply" link in the top right of the post you're responding to. If you respond using that link, the user will be alerted that you responded, otherwise he won't notified at all.
Member 9586495 at 5-Dec-12 7:16am
   
Perhaps it is not the problem of "auto",
but the MS compiler does not yet support
the new Range-For?
ThatsAlok at 5-Dec-12 9:36am
   
you might want to read this http://msdn.microsoft.com/en-us/library/bb531344.aspx
Maximilien at 5-Dec-12 9:59am
   
I think the "auto" keyword is only implemented in VS2012.
Member 9586495 at 5-Dec-12 10:32am
   
"Auto" is an old C/C++ keyword which I have never used.
It has a revival in C++11 and I am trying to understand
its new funcionality.
But it is already supported by VS 2010 in the new way.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It appears that 2010 doesn't support what you're trying to do.
 
Member 9586495 appears to have the right answer from what I can see.
 
See the following for usage in VS 2010:
http://msdn.microsoft.com/en-us/library/dd293667%28v=vs.100%29.aspx[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

As Member 9586495 already noted, is not the auto keyword usage the problem. It is instead the range-based for[^] construct (supported by VS 2012, but not supported by VS 2010, see, for instance: "Does MSVC10 Visual Studio 2010 support C++ range based loops"[^] at Stack Overflow.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,535
1 OriginalGriff 8,295
2 Peter Leow 4,839
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,301


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100