【发布时间】:2016-07-14 13:47:09
【问题描述】:
我正在尝试从 Eclipse 转到 Visual Studio,因为有人告诉我,新手到中级更容易,我从最初的印象中同意。我正在尝试使用类重建经典的井字游戏构建,就像测试文件一样,以感受 IDE,我在 Visual Studio 中收到 28 个错误和 1 个警告,而在 Eclipse 中没有错误和 1 个警告...代码是完全相同的。我完全糊涂了。
需要明确的是,它在 Eclipse 中编译和运行,同时在 Visual Studio 中收到 28 个错误。
为什么会这样?
代码如下
错误列表
Severity Code Description Project File Line Suppression State
Error C2065 'moveCounter': undeclared identifier TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 167
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 10
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 11
Error C2550 'TicTacToe': constructor initializer lists are only allowed on constructor definitions TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 13
Warning C4508 'TicTacToe': function should return a value; 'void' return type assumed TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 14
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 16
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 24
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 29
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 34
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 38
Error C3861 'getPlayerMove': identifier not found TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 48
Error C3861 'checkForWin': identifier not found TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 50
Error C3861 'checkForTie': identifier not found TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 56
Error C3861 'togglePlayer': identifier not found TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 63
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 70
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 98
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 100
Error C2065 'moveCounter': undeclared identifier TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 105
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 116
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 124
Error C2065 'moveCounter': undeclared identifier TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 128
Error C3861 'checkLineForWin': identifier not found TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 138
Error C3861 'checkLineForWin': identifier not found TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 146
Error C3861 'checkLineForWin': identifier not found TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 151
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 157
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 159
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 160
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 161
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:\users\tamend\documents\visual studio 2015\projects\tictactoewithclasses\tictactoewithclasses\tictactoeclassimp.cpp 164
主要
// TicTacToeWithClasses.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "TicTacToe.h"
bool getUserWantToPlay();
int main()
{
while (getUserWantToPlay())
{
TicTacToe game;
game.playGame();
}
return(0);
}
bool getUserWantToPlay()
{
//Assumes no play until response received
char response;
bool invalidResponse = true;
bool play = false;
while (invalidResponse)
{
std::cout << "Would you like to play a new game of TicTacToe? (y/n) " << std::endl;
std::cin >> response;
if (response == 'y')
{
invalidResponse = false;
play = true;
}
else if (response == 'n')
{
std::cout << "No Problem!" << std::endl;
invalidResponse = false;
}
else
{
std::cout << "Please input a proper response (y/n) " << std::endl;
}
}
return play;
}
标题
#ifndef TICTACTOE_H_
#define TICTACTOE_H_
class TicTacToe
{
public:
TicTacToe();
void playGame();
private:
void drawBoard();
void getPlayerMove(char player);
char togglePlayer(char player);
bool checkForWin(char player);
bool checkLineForWin(int square1, int square2, int square3, char player);
bool checkForTie();
char squareArray[9];
int moveCounter;
};
#endif
类实现
//TicTacToe class implementation
#include "TicTacToe.h"
#include "stdafx.h"
#include <iostream>
static const int MinMovesNeededToWin = 5;
static const int MaxMovesInGame = 9;
TicTacToe::TicTacToe()
: squareArray{ '1','2','3','4','5','6','7','8','9' },
moveCounter(0)
{
}
void TicTacToe::drawBoard()
{
//Draws the game board with updated characters for each player
std::cout << "Player 1 (X) - Player 2 (O)" << std::endl << std::endl << std::endl;
std::cout << " | |" << std::endl;
std::cout << " " << squareArray[0] << " | " << squareArray[1] << " | " << squareArray[2] << std::endl;
std::cout << "____|_____|____" << std::endl;
std::cout << " | | " << std::endl;
std::cout << " " << squareArray[3] << " | " << squareArray[4] << " | " << squareArray[5] << std::endl;
std::cout << "____|_____|____" << std::endl;
std::cout << " | | " << std::endl;
std::cout << " " << squareArray[6] << " | " << squareArray[7] << " | " << squareArray[8] << std::endl;
}
void TicTacToe::playGame()
{
char player = 'X';
bool gameOver = false;
do
{
drawBoard();
getPlayerMove(player);
if (checkForWin(player))
{
std::cout << std::endl << "Congratulations player " << player << "! You win!" << std::endl;
gameOver = true;
}
else if (checkForTie())
{
std::cout << "Tie! You should play again to settle the duel!" << std::endl;
gameOver = true;
}
player = togglePlayer(player);
}while (!gameOver);
drawBoard();
}
void TicTacToe::getPlayerMove(char player)
{
//Gets player move and stores in board array for display through next iteration
bool playerMoveFound = false;
char playerTurn = '0';
char playerMove = '0';
if (player == 'X')
{
playerTurn = '1';
}
else
{
playerTurn = '2';
}
while (playerMoveFound == false)
{
std::cout << "Player " << playerTurn << " please make a move" << std::endl;
std::cin >> playerMove;
for (int counter = 0; counter < 9; counter++)
{
//If finds the array number makes the change to the iteration...prevents x or o movement
if (playerMove == squareArray[counter] && playerMove != 'X' && playerMove != 'O' && playerMove != 'x' && playerMove != 'o')
{
squareArray[counter] = player;
playerMoveFound = true;
//Move counter implemented to streamline check for win/check for tie
moveCounter++;
}
}
if (playerMoveFound == false)
{
std::cout << "Invalid player move..." << std::endl;
}
}
}
char TicTacToe::togglePlayer(char player)
{
player = player == 'X' ? 'O' : 'X';
return(player);
}
bool TicTacToe::checkForWin(char player)
{
bool playerWin = false;
if (moveCounter >= MinMovesNeededToWin)
{
// Only need to check for current player
// Only need to check until we have a win
// Tests rows for win
for (int rowCounter = 0; !playerWin && rowCounter < 8; rowCounter += 3)
{
playerWin = checkLineForWin(rowCounter, rowCounter + 1, rowCounter + 2, player);
}
// Tests columns for win
for (int columnCounter = 0; !playerWin && columnCounter < 3; columnCounter++)
{
playerWin = checkLineForWin(columnCounter, columnCounter + 3, columnCounter + 6, player);
}
// Tests diagnols for win
playerWin |= (checkLineForWin(0, 4, 8, player) | checkLineForWin(2, 4, 6, player));
}
return(playerWin);
}
bool TicTacToe::checkLineForWin(int square1, int square2, int square3, char player)
{
return squareArray[square1] == player &&
squareArray[square2] == player &&
squareArray[square3] == player;
}
bool TicTacToe::checkForTie()
{
return moveCounter == MaxMovesInGame;
}
【问题讨论】:
-
你的类实现文件可能没有编译,因为
stdafx.h没有被首先包含;更改包含的顺序,以便将stdafx.h包含在之前TicTacToe.h。 -
@ildjarn 哇...修复了!哈哈谢谢你!请问为什么会这样?请把它作为答案发布,这样我就可以给你信用了。究竟什么是 stdafx.h?明明是头文件,但是里面有什么?我在 Eclipse 中没有看到。
-
它与预编译的头文件一起使用。见stdafx.h: When do I need it?
-
除了问题的答案 (PCH) 之外,还值得记住的是,不同的编译器会检测到不同的问题并发出不同的警告。编译器也有不同的错误,一个可能会接受另一个拒绝的损坏代码。在高警告级别使用多个编译器进行测试通常是一件好事(我经常使用 clang、gcc 和 VC++ 编译所有代码,这样做会捕获错误)。
标签: c++ eclipse visual-studio c++11 visual-studio-2012