C
cpisz
How can I read and write an enum value to a file?
The code in question:
Cell temp;
temp.m_x = x;
temp.m_y = y;
file >> temp.m_type;
The error:
c:\Documents and Settings\christopher.pisz\My Documents\Visual Studio
Projects\parser\maze.cpp(108): error C2679: binary '>>' : no operator
found which takes a right-hand operand of type 'Cell::CELLTYPE' (or
there is no acceptable conversion)
The Cell defination:
class Cell
{
public:
enum CELLTYPE{ START, PASSABLE, IMPASSABLE, END, UNKNOWN};
Cell();
~Cell();
void Display();
int m_x,
m_y;
CELLTYPE m_type;
};
file is of course an ifstream declared and opened earlier...
At first I wanted to have the enumerated names in the input file so
that it would be reader friendly, then I tryed just using the number
values after I gave up on converting strings to enumerated values using
a long google search and not liking the ideas presented. Now I can't
even seem to read in numbers to an enumerated type. enum is seeming
less and less friendly. I thought the whole idea of enum was to make
things friendly to the coder and reader...
What is your ideas on a solution to this problem? Besides the obvious
of not using enum at all, but I don't like that, because m_type is not
an int, it is a cell type.
Is there an effiicient way to read in strings from the file such as
START, PASSABLE, etc. and convert them to an enumerated type?
If not, is there a way to just read in a number such as 0 and convert
it to the enumerated type START without using a switch statement and
having to edit it every time a new type is introduced?
The code in question:
Cell temp;
temp.m_x = x;
temp.m_y = y;
file >> temp.m_type;
The error:
c:\Documents and Settings\christopher.pisz\My Documents\Visual Studio
Projects\parser\maze.cpp(108): error C2679: binary '>>' : no operator
found which takes a right-hand operand of type 'Cell::CELLTYPE' (or
there is no acceptable conversion)
The Cell defination:
class Cell
{
public:
enum CELLTYPE{ START, PASSABLE, IMPASSABLE, END, UNKNOWN};
Cell();
~Cell();
void Display();
int m_x,
m_y;
CELLTYPE m_type;
};
file is of course an ifstream declared and opened earlier...
At first I wanted to have the enumerated names in the input file so
that it would be reader friendly, then I tryed just using the number
values after I gave up on converting strings to enumerated values using
a long google search and not liking the ideas presented. Now I can't
even seem to read in numbers to an enumerated type. enum is seeming
less and less friendly. I thought the whole idea of enum was to make
things friendly to the coder and reader...
What is your ideas on a solution to this problem? Besides the obvious
of not using enum at all, but I don't like that, because m_type is not
an int, it is a cell type.
Is there an effiicient way to read in strings from the file such as
START, PASSABLE, etc. and convert them to an enumerated type?
If not, is there a way to just read in a number such as 0 and convert
it to the enumerated type START without using a switch statement and
having to edit it every time a new type is introduced?