views:

207

answers:

2

I get the following error with VS2008: Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)

When casting a down casting a ClassA to ClassA_1 and ClassA_1 is a templated class that received an enum for parameter such as:

THIS IS AN EDIT OF MY QUESTION WHICH RE-USE AN ANSWER BELOW BUT MODIFED TO CAUSE THE PROBLEM I AM HAVING, here we go:

Ok i have been able to reproduce my error with this code:

class ClassA
{
public:
    virtual ~ClassA(){}
};

template <class Param1 = void*> class ClassB : public ClassA {
public:
    //constructor
    ClassB(Param1 p1 = NULL)
    {
        _p1 = p1;
    }
    //ClassB(const ClassB<Param1>& ref);
    Param1 _p1;
    ~ClassB(){}
};

enum lolcakes {
    cakeisalie,
};


ClassA* ptr = new ClassB<lolcakes>(lolcakes::cakeisalie);

ClassB<lolcakes>* a1 = (ClassB<lolcakes>*)ptr;
+2  A: 

There are so many syntax errors here, I don’t know where to begin. Next time, please post the actual code you used.

For starters, I’m assuming that you meant to write this:

ClassA<myenum>* a = new ClassA_1<myenum>();

In other words, a is a pointer and its type is ClassA<myenum>*, not merely ClassA (and we’ll ignore the missing argument to the constructor).

Now, your cast syntax is wrong in both cases. The parentheses need to go around the type only. But better use a static_cast anyway:

ClassA_1<myenum>* a1 = static_cast<ClassA_1<myenum>*>(a);

This works.

UPDATE After question edit:

The important error is in this line:

ClassB(Param1 p1 = NULL)

you cannot use NULL as the default parameter since your Param1 type is not a pointer – it’s an enum (strictly speaking this should work since NULL is defined as being equal to 0 in C++, but it’s a logical error nonetheless). Instead of making the parameter optional, a better alternative would be to overload the constructor. Alternatively, the following also works:

ClassB(Param1 p1 = Param1())

This uses the default value for the type Param1.

There’s an additional error in the code:

ClassA* ptr = new ClassB<lolcakes>(lolcakes::cakeisalie);

Enum constants don’t work like that in C++: They don’t create an own namespace, hence their usage cannot be qualified. Instead, omit the enum’s name:

ClassA* ptr = new ClassB<lolcakes>(cakeisalie);

Finally, please don’t use C-style casts, ever. Always replace them with the appropriate C++-style casts. In your case, replace

ClassB<lolcakes>* a1 = (ClassB<lolcakes>*)ptr;

with

ClassB<lolcakes>* a1 = boost::polymorphic_downcast<ClassB<lolcakes>*>(ptr);
// or
ClassB<lolcakes>* a1 = static_cast<ClassB<lolcakes>*>(ptr);
Konrad Rudolph
Consider using dynamic_cast or at least boost::polymorphic_downcast.
Noah Roberts
@Noah: (I scrapped my last comment.) You’re right, `boost::polymorphic_downcast` would be better. But `dynamic_cast` wouldn’t since we *know* the type of `a`. `dynamic_cast` signals “I don’t know what type the object has”, while `static_cast` signals that “I assume a type and if that assumption is wrong the this is a bug”. `dynamic_cast` should only be used if we *don’t know* for sure which type to expect, and if we subsequently check for null.
Konrad Rudolph
i have posted new code (in the question) which suffer from the issue.
JP
@Konrad - I don't use those assumptions when I see those casts and I wouldn't suggest it either. I suppose you could reword your assumption about static cast to, "This cast must be as fast as possible and if it's erroneous I am willing to allow undefined behavior to effect my customer. I hope that it doesn't and that I've adequately made sure it won't but I know if it does that really horrible, random things could happen to the user and I've left myself no way to recover." Not saying it doesn't have its uses but using static_cast to downcast really should be always met with that attitude.
Noah Roberts
@JP: See updated answer.
Konrad Rudolph
@Noah: The operators *have* these meanings. A `dynamic_cast` definitely describes the wrong semantics here.
Konrad Rudolph
you got it.... =NULL is bad, it was there since my template had default time such as template <class Param1 = void*>,, but the = NULL are not needed so, thanks a lot!
JP
@Konrad - I don't know why you think that is the case. I could quote the standard to prove that they are not defined that way but I'd hope you realize this and can tell the difference between what you interpret them to mean when you see them and what they're actually defined to do.
Noah Roberts
@Noah: please quote away. I don’t see how my interpretation is a stretch of the definition. I respect their respective defined semantics to the letter. I reserve the right to use common sense, however. Using `dynamic_cast` when you *know* what to expect is just as stupid as checking for `null` in Java before every single method call, or double-checking whether a loop index `i` is `i >= 0 and i < N` before every subscript access. That’s called cargo-cult programming.
Konrad Rudolph
For the enum thing: It's a VS extension.
DeadMG
dynamic_cast is described in 5.2.7, static_cast is described in 5.2.9. I think it probably better, since quoting those sections would require more than allowed here, that you simply point to those parts of the standard that define them how you've said they are defined.
Noah Roberts
+1  A: 
class ClassA
{
    virtual ~ClassA(){}
};

template <class Param1> class ClassB : public ClassA {
public:
    //constructor
    ClassB(Param1 p1)
        : _p1(p1) {}
    ClassB(const ClassB<Param1>& ref);
    Param1 _p1;
    ~ClassB(){}
};

enum lolcakes {
    cakeisalie,
};

ClassA* ptr = new ClassB<lolcakes>(lolcakes::cakeisalie);
ClassB<lolcakes> a = (ClassB<lolcakes>(lolcakes::cakeisalie));
ClassB<lolcakes> a1 = a;
DeadMG
lolcake​​​​​​​​s
GMan
i have posted new code (in the question) which suffer from the issue.
JP