Invoke enum class from header file to cpp file

Good evening ladies and gentlement.

I have been working a project in C++. I have TTTMain.cpp file that has all the function calls, TTTFuntions.cpp that has all the functions, I have TTT.h file that has all the prototypes and variables and additionally I have Winner.h that has enum class Winner declaration in it. Here is my block of codes:

Winner.h file:
1
2
3
4
5
6
7
8
9
10
#ifndef winner
#define winner

enum class Winner
{
    Empty,
    Computer,
    Player
};
#endif  


TTT.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[output][/output]
#ifndef TTT
#define TTT


#include <iostream>
#include <cmath>
#include "Winner.h"
using namespace std;

class TTT
{
public:
Winner gameSquares[] = { Empty, Empty,Empty, Empty, Empty, Empty, Empty, Empty, Empty };
int movesMade = 0;

void HowTo();
void PlayerTurn();
bool FirstGo();
int GetPossibleMoves(int possible_index[9], Winner who, const Winner *const board = gameSquares);
int GetWinIndex(const Winner who);
void ComputerTurn();
void PrintBoard();

};
#endif

My question is when I compile this gives me error on

Winner gameSquares[] = { Empty, Empty,Empty, Empty, Empty, Empty, Empty, Empty, Empty };

with saying "invalid use of non-static data data member"
and It says "Empty was not declared in this scope."


I know calling enum is very very trick. How possibly could I solve this problem?I have been trying to figure it out for a while but I gave up on! Thanks for your time and attention
When you use enum class you have to specify the name of the enumeration when accessing the enumerators.
 
Winner gameSquares[] = { Winner::Empty, Winner::Empty, ..., Winner::Empty };

Peter87- I tried it but it does not work unfortunately It still gives exactly the same error.
I think you have to specify the array size explicitly because it's a member.
 
Winner gameSquares[9] = { Winner::Empty, Winner::Empty, ..., Winner::Empty };


You get the error "invalid use of non-static data member" because you can't use non-static data members as default arguments. As a workaround you could make null a default argument and then test if board is null inside the function or you could provide two versions of the function (one with 1 argument and one with 2 arguments).
Topic archived. No new replies allowed.