0

I am attempting to move over to visual studio from Eclipse as I have been told it is easier for novice to intermediate and I agree from my initial impression. I am trying to rebuild a classic TicTacToe build with classes just as a test file to get a feel for the IDE and i am receiving 28 errors and 1 warning in Visual Studio compared to no errors and 1 warning in Eclipse...The code is exactly the same. I am utterly confused.

To be clear it compiles and runs in Eclipse while receiving 28 errors in Visual Studio.

Why is this happening?

Code below

Error list

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

Main

// 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;
}

Header

#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

Class Implementation

//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;

}
StormsEdge
  • 854
  • 2
  • 10
  • 35
  • It's possible that your class implementation file is not compiling because `stdafx.h` is not included first; change the order of includes so that `stdafx.h` is included _before_ `TicTacToe.h`. – ildjarn Jul 14 '16 at 13:51
  • @ildjarn Wow...that fixed it! haha thank you! May I ask why that would happen? Please post that as an answer so I can give you credit for it. Exactly what is stdafx.h? Obviously it's a header file, but what's in it? I didn't see that in eclipse. – StormsEdge Jul 14 '16 at 13:55
  • 1
    It is used with precompiled headers. See [stdafx.h: When do I need it?](http://stackoverflow.com/questions/676385/stdafx-h-when-do-i-need-it) – Bo Persson Jul 14 '16 at 13:58
  • 3
    See [Precompiled Header Files](https://msdn.microsoft.com/en-us/library/h552b3ca.aspx) as well as [Using Precompiled Headers in a Project](https://msdn.microsoft.com/en-us/library/3dxawkbx.aspx). Also highly recommended: [Does Visual Studio Rot the Mind?](http://charlespetzold.com/etc/DoesVisualStudioRotTheMind.html). – IInspectable Jul 14 '16 at 13:59
  • 1
    Besides the answer to the question (PCH), it's also worth remembering that different compilers detect different problems and issue different warnings. Compilers also have different bugs and one may accept broken code that another rejects. Testing with multiple compilers at high warning levels is usually a good thing (I rutinely compile all code with both clang, gcc and VC++ and doing so catches bugs). – Jesper Juhl Jul 14 '16 at 15:29

1 Answers1

2

With Visual C++, when precompiled headers are enabled (which they usually are by default, named stdafx.h) your PCH must be the first header included in each translation unit. If you forget to include the PCH altogether, you get a reasonable C1010 error telling you so; however, there is unfortunately no sensible diagnostic output to tell you why things are failing when you merely include headers in the wrong order.

Change the order of your includes in the class implementation TU so that stdafx.h is included before TicTacToe.h. Further reading about precompiled headers specifically in the context of VC++ can be found here.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • 1
    Alternatively, specify the [/FI (Name Forced Include File)](https://msdn.microsoft.com/en-us/library/8c5ztk84.aspx) compiler option. This is particularly interesting when compiling 3rd party library code that you cannot modify, but still want to use precompiled headers with. – IInspectable Jul 14 '16 at 14:06