开发者

C++ pass enum to object constructor

开发者 https://www.devze.com 2023-03-22 09:12 出处:网络
Say I have the following: Foo::Foo() { value = 25; //default constructor... } Foo::Foo(Enum bar) { value = (int)bar; //purpose is to allow an integer to take enum constant\'s开发者_JS百科 integer va

Say I have the following:

Foo::Foo() {
value = 25; //default constructor...
}

Foo::Foo(Enum bar) {
value = (int)bar; //purpose is to allow an integer to take enum constant's开发者_JS百科 integer value.
}

from...

enum Enum 
{
A = 25,
B = 50,
}

class Foo 
{
public:
    Foo();
    Foo(Enum bar);
private:
int value;
}

Yet, if I do the following:

Enum bar = A; //A = 25

Foo * foo = new Foo(A); //error: "undefined reference to Foo::Foo(Enum)"

This is in Eclipse CDT 3.6. Why is this happening? Is there anything I can do about this to solve the problem?


After fixing a few syntax errors (extra , in Enum definition, missing ; after Enum and Foo definitions), your code was run-able in gcc. Check here: http://www.ideone.com/GZdNM

I don't really understand what you're trying to do with the enum, but for sure, calling it Enum is probably not a great idea...


It sounds like you forgot to link foo.C into your final application.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号