Casting enum to other enum?

Hi,

I have two classes, Board and PuzzleSearch. Both contains a private enum named MOVE_DIRECTIONS with the same values MOVE_UP, MOVE_DOWN, MOVE_RIGHT and MOVE_LEFT. The enum in board is used to move tiles based on player input and the one in PuzzleSearch are used for generating a solution sequence to the puzzle. When the solution has been generated I want to return it from the PuzzleSearch class to the Board class. The solution is stored in a vector like this:

std::vector<MOVE_DIRECTIONS> solution;

Problem is I cannot write this line in the Board class:

std::vector<MOVE_DIRECTIONS> solution = search.GetSolution();

It complains that the PuzzleSearch::MOVE_DIRECTIONS cannot be converted into Board::MOVE_DIRECTIONS. Any way around this?

/Robin
Maybe to declare this enumeration outside the classes?

No I dont want to declare stuff in the global namespace. There must be another way.
Another way is to copy one vector into another and use type casting for each element of the source vector.
Not really sure what you mean there.
As I understand this statement performs assigning one vector to another and these vectors have different types of elements.

std::vector<MOVE_DIRECTIONS> solution = search.GetSolution();

So you need copy yourself all elements from one vector to another using casting to each element of the source vector.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct puzzle
{
    enum move_direction { LEFT, RIGHT, UP, DOWN } ;
    const std::vector<move_direction>& solution() const { return solution_seq ; }
    // ...
    private:
        std::vector<move_direction> solution_seq ;
        // ...
};

struct board
{
    board( const puzzle& search ) : solution_seq ( search.solution() ) {}
    // ...
    private:
        std::vector<puzzle::move_direction> solution_seq ;
        // ...
};


I have the classes in different header files and I don't want to include one inside the other just to get access to the public enum. Any other solutions?
1
2
// common_types.h
struct common_types { enum move_direction { LEFT, RIGHT, UP, DOWN } ; } ;


1
2
3
4
5
6
7
8
9
10
// puzzle.h
#include "common_types.h"
struct puzzle : common_types
{
    const std::vector<move_direction>& solution() const { return solution_seq ; }
    // ...
    private:
        std::vector<move_direction> solution_seq ;
        // ...
};


1
2
3
4
5
6
7
8
9
10
// board.h
#include "common_types.h"
struct board : common_types
{
    board( const puzzle& search ) : solution_seq ( search.solution() ) {}
    // ...
    private:
        std::vector<move_direction> solution_seq ;
        // ...
};


Why didn't I think of that? Silly me ;)

Thanks!
Topic archived. No new replies allowed.