I just started messing around with classes and have been trying to figure out this error for an hour or so. Here is the compiler error:

g++ player.cpp tictactoe.cpp
TicTacToe.h:15: error: expected constructor, destructor, or type conversion before ';' token
player.cpp:26: error: 'TicTacToe' has not been declared
player.cpp: In member function 'void Player::NextMove(int)':
player.cpp:35: error: request for member 'setValue' in 'toe', which is of non-class type 'int'

Player.cpp

#include <iostream>
#include "TicTacToe.h"
using namespace std;
class Player
{
        public:
 
                Player(char *pname, int pnumber)
                {
                        strcpy(name, pname);
                        number = pnumber;
                }
                int GetIndex() { return number; }
                char * GetName() { return name; }
                void NextMove(TicTacToe toe)
                {
                        int row, col;
                        do
                        {
                                cout << "\nPlayer " << number << ": Enter the Row and Column for your next move: ";
                                cin >> row >> col;
                        }
                        while (toe.setValue(row, col, number) == false);
                }
        private:
                char name[];
                int number;
};

TicTacToe.cpp

#include <iostream>
using namespace std;
 
 
class TicTacToe
{
        public:
                void print();   //prints out the gameboard at any time
                int getStatus();        //returns tie, win for either player, or neither
                TicTacToe();
                bool setValue(int row, int col, int player)
                {
                        if(row < 4 && col < 4 && row > 0 & col > 0)
                        {
                                if(isEmpty(row, col) == true)
                                {
                                        char a;
                                        if(player == 1) a = 'x';
                                        else a = 'o';
                                        board[row-1][col-1] = a;
                                        return true;
                                }
                                else
                                {
                                        cout << "\nSpace already occupied. Try again.\n";
                                        return false;
                                }
                        }
                        else
                        {
                                cout << "\nInvalid position.  Try again.\n";
                                return false;
                        }
                }
        private:
        bool isEmpty(int row, int col)
        {
                if (board[row-1][col-1] == '\0') return true;
                else return false;
        }
        char board[3][3];
};
 
TicTacToe::TicTacToe()
{
        int x, y;
        for(x = 0; x < 3; x++)
        {
                for(y = 0; y < 3; y++)
                {
                        board[x][y] = '\0';
                }
        }
}

TicTacToe.h

#include <iostream>
using namespace std;
 
TicTacToe();
void print();
int getStatus();
bool setValue(int, int, int);

This is a TicTacToe game(nowhere near finished, but shouldn't be too hard after I knock out my syntax errors)

Thanks for the help

Recommended Answers

All 5 Replies

Move the class declaration of TicTacToe from the TicTacToe.cpp file to the TicTacToe.h file.
Also in the TicTacToe.cpp file add a

#include "TicTacToe.h"

line.
Delete the

TicTacToe();

line from the TicTacToe.h file also.

As a noob, I'm trying to nail down some specifics as well.

In the above code, wouldn't it be more memory conservative if the definition of bool setvalue(...) was not "inline" but defined in player.cpp?

tictactoe::tictactoe.setvalue(...)
{
...
}

I ask this since i thought I read that there will be less copies made this way.
do i have that correct ?

This is where your experiece and expertise in the field of Object Oriented practices comes into play and you would be faced with such situations during project development.

Normally one liner functions are kept inline since inline functions are pasted at the place of their call and not called like normal functions.

Hence accessor functions like getValue( ) , setValue( ) etc. are normally made inline.

In the above code, wouldn't it be more memory conservative if the definition of bool setvalue(...) was not "inline" but defined in player.cpp?

tictactoe::tictactoe.setvalue(...)
{
...
}

In this case also the setvalue function is not inline. Even if you declare it in the header file, you have to use the inline keyword to make it inline. So moving it to the player.cpp file will not make a difference. But usually all definitions are moved to the .cpp file to make the header file less bulky and to hide the implementation.

I ask this since i thought I read that there will be less copies made this way.
do i have that correct ?

Yes. If the function is not inline the function will be called at runtime instead of embedding it during compile time (copied everywhere it is needed to be called). But using inline will make the executable file size more bulky, although the execution will be faster. So usually only small functions like get or set functions are declared as inline.

> char name[];
This isn't some "magic" variable length array you can just scribble chars into with strcpy.
Making it char* won't help much either, unless you want to manage memory yourself.

Personally, I'd go with std::string name; Then your member functions can be like this

Player( std::string pname, int pnumber)
                {
                        name = pname;
                        number = pnumber;
                }
                std::string GetName() { return name; }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.