Compiling a text game

Started by
3 comments, last by Faded-Maximus 17 years, 11 months ago
Hey, I am trying to compile a game I wrote in Visual C++ 6. I get this as an error message: Compiling... tic-tac-toe.cpp tic-tac-toe.cpp(213) : error C2374: 'move' : redefinition; multiple initialization tic-tac-toe.cpp(196) : see declaration of 'move' Error executing cl.exe. I realize in my code that I have redefined move = 0 twice, once on line 196 and once on line 213. Why isn't this allowed? and how can I prevent this from happening? Any help is greatly appreciated.

	// If computer can win on next move, make that move
	for(int move = 0; move < board.size(); ++move)
	{
		if (isLegal(move, board))
		{
			board[move] = computer;
			if (winner(board) == computer)
			{
				cout << move << endl;
				return move;
			}
			// done checking this move, undo it
			board[move] = EMPTY;
		}
	}

	// If human can win on enxt move, block that move
	char human = opponent(computer);
	for(int move = 0; move < board.size(); ++move)
	{
		if (isLegal(move, board))
		{
			board[move] = human;
			if (winner(board) == human)
			{
				cout << move << endl;
				return move;
			}
			// done checking this move, undo it
			board[move] = EMPTY;
		}
	}
Advertisement
using move that way should be legal, they are not in the same scope. Do you have move defined anywhere before this that would be in the scope of move?

theTroll
Quote:Original post by Faded-Maximus
Why isn't this allowed?

it is allowed in standard c++. the problem is that vc++ 6 is pre-standard and a variable declared in a for loop doesn't go out of scope when exiting the loop like it's supposed to.
Quote:
how can I prevent this from happening?

i'd suggest upgrading. visual c++ 2005 express is a free download.
This space for rent.
Edit: Beaten to it. Oh well.

The problem is that you're using VC 6, which does not correctly follow the C++ standard.
If you're determined to continue using VC 6 for some reason (which you probably shouldn't be - more recent and much better compilers, both from Microsoft and others, are available for free), then you can solve the problem by defining the move variable once, before the first for loop, and then only initializing it within each loop, rather than actually declaring it within each loop, ie,
int move;for (move = 0; ...)    ......for (move = 0; ...)    ...


But otherwise, go and download VC++ Express 2005.

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Shrugs - just another compiler to get used too...

Anyways, thanks for the advice I will download it and give it a try.

This topic is closed to new replies.

Advertisement