nonstandard extension 'for each' statement

This is for my CIS class and a method that is part of a blackjack game. The issue is that every "for each" statement is throwing an error, even though it seems to be written correctly. In fact, this statement was given to us by our professor to use and does not throw an error for him, but does for me.

When I build it shows the error: error C4496: nonstandard extension 'for each' used: replace with ranged-for statement.

Additionally, the "each" "in" and ")" are burning(showing error). "each" says expected a ')' the "in" says expected a ';' and the ")" says expected an expression. Any help would be greatly appreciated. Thanks in advance.



1
2
3
4
5
6
7
8
9
10
11
  string showCards( vector<Card>cards) 
{
	string output = " ";
	
	for each (Card c in cards)
	{
		output += c.toString() + " ";
	}

	return output;
}
That isn't C++, looks more like C# but in C# the keyword is "foreach" not "for each".

You're looking for:
1
2
3
4
for (Card c : cards)
{
     output += c.toString();
}


If your code is const-correct, then you should rather do:
1
2
3
4
5
6
7
8
9
string showCards(const vector<Card>& cards)
{
    // ...
    for (const Card& c : cards)
    {

    }
    // ...
}
Last edited on
That's odd, because the professor showed it the way I posted it without an error. My visual studio also will automatically format the statement if I type foreach and hit tab, but why if it isn't even c++?

To be clear, and bear with me because I am obviously learning still, the ":' is a scope resolution that is tying c to cards?
I did a bit more searching... your professor is using Microsoft's extension of the C++ language called "C++\CLI".

From Visual Studio, do File --> New --> Project.
Choose Visual C++ --> CLR --> CLR Console Application.

Then, do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"

#include <vector>
#include <iostream>

using namespace System;

int main(array<System::String ^> ^args)
{
	using std::vector;
	using std::cout;

	vector<int> vec;
	vec.push_back(3);
	
	for each (int i in vec)
	{
		cout << i << '\n';
	}

    return 0;
}

(remove the stdafx.h line if it gives errors)


See: https://docs.microsoft.com/en-us/cpp/dotnet/for-each-in?view=vs-2019

the ":' is a scope resolution that is tying c to cards?
In my example, the : is just special syntax used in a for-each loop in standard C++. It's not the scope resolution operator.
See: https://en.cppreference.com/w/cpp/language/range-for

Feel free to ask more about C++\CLI in the future, but note that if you do continue to use C++\CLI and not standard C++, make sure you say this in your original post, otherwise there will be more confusion in the future.
Last edited on
Thank you for the help. I know that we weren't using a console application, so I did a bit more looking. Apparently the for each was acceptable with Visual Studio 15 but I am using 17 and it does not want you to write it that way anymore. The fix you gave me is actually exactly how my professor said it should be done now as well.

I'm having some other frustrating issues as well, but I think I am going to put that into a new topic so we don't clutter this one up.

Thanks again.
Registered users can post here. Sign in or register to post.