Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My actual programming workbench is MS Visual Studio C++ 2010 SP1Rel.

I want to code something like:

C++
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
Comments
Richard MacCutchan 5-Dec-12 6:45am    
but MS says no.

What exactly do they say, and where do they say it?
sja63 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 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.
sja63 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 5-Dec-12 9:36am    
you might want to read this http://msdn.microsoft.com/en-us/library/bb531344.aspx

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[^]
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900