不同的 IDE 生成 0 个错误与 28 个错误
Different IDEs generating 0 errors vs 28 errors
我正在尝试从Eclipse转移到Visual Studio,因为我被告知新手到中级更容易,我从最初的印象中同意。 我正在尝试使用类重建经典的 TicTacToe 构建,就像测试文件一样,以了解 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:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 167
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 10
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 11
Error C2550 'TicTacToe': constructor initializer lists are only allowed on constructor definitions TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 13
Warning C4508 'TicTacToe': function should return a value; 'void' return type assumed TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 14
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 16
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 24
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 29
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 34
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 38
Error C3861 'getPlayerMove': identifier not found TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 48
Error C3861 'checkForWin': identifier not found TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 50
Error C3861 'checkForTie': identifier not found TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 56
Error C3861 'togglePlayer': identifier not found TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 63
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 70
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 98
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 100
Error C2065 'moveCounter': undeclared identifier TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 105
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 116
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 124
Error C2065 'moveCounter': undeclared identifier TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 128
Error C3861 'checkLineForWin': identifier not found TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 138
Error C3861 'checkLineForWin': identifier not found TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 146
Error C3861 'checkLineForWin': identifier not found TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 151
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 157
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 159
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 160
Error C2065 'squareArray': undeclared identifier TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.cpp 161
Error C2653 'TicTacToe': is not a class or namespace name TicTacToeWithClasses c:userstamenddocumentsvisual studio 2015projectstictactoewithclassestictactoewithclassestictactoeclassimp.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;
}
使用 Visual C++ 时,当启用预编译标头(默认情况下,它们通常名为 stdafx.h
)时,PCH 必须是每个翻译单元中包含的第一个标头。如果您忘记完全包含 PCH,则会收到一个合理的 C1010 错误告诉您;但是,不幸的是,没有合理的诊断输出来告诉您为什么当您仅以错误的顺序包含标头时会失败。
更改类实现 TU 中包含的顺序,以便在 TicTacToe.h
之前包含stdafx.h
。有关 VC++ 上下文中的预编译标头的进一步阅读,请参阅此处。
相关文章:
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 错误:在"生成字符串"之前应为";"
- g++ 为 transform() 的参数生成错误
- 生成错误 - "." 标记之前的预期主表达式
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- C++生成文件错误"implicit entry/start for main executable"
- Visual Studio 2013,错误 MSB8020:找不到 Visual Studio 2010 的生成工具
- C++/生成文件错误:未定义对"main"的引用
- std:中的错误:生成normal_distribution时随机
- GL_INVALID_VALUE错误生成.纹理尺寸无效.在立方体贴图纹理 427*240*6 上
- 生成文件错误 :生成: *** [主要] 错误 1
- 链接的错误/生成文件
- 错误:生成Mesos中无法识别的命令行选项"-Wno无效源编码"[-Weror]
- 无法在 Visual Studio 2013 中运行非常基本的代码 - 运算符">>"错误/生成错误
- 错误/usr/include/string.h:652:42:错误:生成caffe时未在此作用域中声明“memcpy”
- 多函数定义错误生成文件