?
Solved

Enumerated types and serialization

Posted on 2009-04-16
6
Medium Priority
?
187 Views
Last Modified: 2013-11-20
Hiya

If I set a variable to be an enumerated type I cannot serialize the valriable because I get the following compilation error:

Error      1      error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'MyEnum'' (or there is no acceptable conversion)      

and

Error      2      error C2440: '=' : cannot convert from 'int' to 'MyEnum'      

Any ideas how I can get round this?

John

0
Comment
Question by:IssacJones
  • 3
  • 3
6 Comments
 
LVL 19

Expert Comment

by:alb66
ID: 24156567
Simply cast your variable to int;
0
 

Author Comment

by:IssacJones
ID: 24156596
hiya alb66

That's what I tried first but I'm afriad doesn't work e.g. if the variable is x, then

ar >> (int) x

still throws a compilation error.
0
 
LVL 19

Accepted Solution

by:
alb66 earned 1000 total points
ID: 24156673
Try the following:

enum MyEnum { a, b, c };
 
class CTestSerDoc : public CDocument
{
protected: // creare solo da serializzazione.
	CTestSerDoc();
	DECLARE_DYNCREATE(CTestSerDoc)
 
// Attributi
public:
	MyEnum m_eMyEnum;
}
 
 
void CTestSerDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar << (int)m_eMyEnum;
	}
	else
	{
		int temp;
		ar >> temp;
		m_eMyEnum = (MyEnum)temp;
	}
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:alb66
ID: 24156743
A more elegant way:
void CTestSerDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar << (int)m_eMyEnum;
	}
	else
	{
		ar >> (int&)m_eMyEnum;
	}
}

Open in new window

0
 

Author Closing Comment

by:IssacJones
ID: 31570914
Thanks alb!
0
 

Author Comment

by:IssacJones
ID: 24156800
I like the last solution better. thanks.

Interestingly, you don't need to cast when storing. only when loading.

John
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question