tags:

views:

129

answers:

2

Hi, I wrote class like this:

#pragma once
#include "stdafx.h"

struct Date
{
private:
int day;
int year;
enum Month {jan = 1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
Month* month;
enum date_state 
{
 good,err_flag, bad_day, bad_month, bad_year,
};
//I would like to define converting operator from enum to char
Date::date_state::operator char()
{
 return err_flag;
}
date_state err_state;
void clear(date_state state = good);
date_state rdstate() const;
void check_day(const int d)const;
void check_month()const;
void check_year()const;
public:
Date(const int d,const Date::Month& m, const int y);

};

and basically it doesn't work. Thank you for any help with this problem.

A: 

Enum is not a class/struct, hence you can't define a conversion operator for it.

I would suggest writing a global-scope (within a namespace) function to make the proper conversions.

Something along the lines of:

char convert (Month m) {
  switch (m) {
    case (jan): return 'j';
    case (feb): return 'f';
    default:    return 'x';
  }
}
rmn
Thanks---------------------------------
There is nothing we can do
But I always thought that enum is fully fledged type in C++. Obviously isn't.P.S.Any chance that inserting block of code in this website will be done on something like [code=cpp]some code here[/code]. This buisness with for spaces drives me bonkers.
There is nothing we can do
c++0x will add "fully fledged enmus". Well at least partially ;-)
drhirsch
A: 

You can't declare member functions for enum date_state, because it is an enum, but you could do so for class Date:

class Date {
...
    enum date_state 
    {
        good, bad_day, bad_month, bad_year,
    } err_flag;

    operator char() {
        return err_flag;
    }
}

But would rather recommend using a normal member function instead, because a conversion operator might easily be used accidently.

drhirsch