开发者

Enum in C++: how to pass as parameter?

I have in my class definition the following enum开发者_StackOverflow社区:

static class Myclass {
     ...
  public:    
     enum encoding { BINARY, ASCII, ALNUM, NUM };
     Myclass(Myclass::encoding);
     ...
}

Then in the method definition:

Myclass::Myclass(Myclass::encoding enc) {
    ...
}

This doesn't work, but what am I doing wrong? How do I pass an enum member correctly, that is defined inside a class for member methods (and other methods as well)?


I'm not entirely sure why you're using "static class" here. This boilerplate works just fine for me in VS2010:

class CFoo
{
public:
    enum Bar { baz, morp, bleep };
    CFoo(Bar);
};

CFoo::CFoo(Bar barIn)
{
    barIn;
}


This code is fine:

/* static */ class Myclass
{
  public:    
     enum encoding { BINARY, ASCII, ALNUM, NUM };
     Myclass(Myclass::encoding); // or: MyClass( encoding );
     encoding getEncoding() const;
}; // semicolon

Myclass::Myclass(Myclass::encoding enc)
{    // or:     (enum Myclass::encoding enc), they're the same
     // or:     (encoding enc), with or without the enum
}

enum Myclass::encoding Myclass::getEncoding() const
//or Myclass::encoding, but Myclass:: is required
{
}

int main()
{
    Myclass c(Myclass::BINARY);
    Myclass::encoding e = c.getEncoding();
}

Update your question with the real code and errors you're getting so we can solve real problems instead of fake ones. (Give us a * compilable* example that reproduces your problem.)


class Myclass {
     ...
public:    
     enum encoding { BINARY, ASCII, ALNUM, NUM };
     Myclass(enum Myclass::encoding);
     ...
}

Myclass::Myclass(enum Myclass::encoding enc) {
     ...
}

Just just forgot the enum keyword in the parameter.


Remove the static. Generally, mentioning the exact error will help you get better answers.


See this:

C++ pass enum as parameter

You reference it differently depending on the scope. In your own class you say

Myclass(encoding e);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜