14

I have in my class definition the following enum:

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)?

polemon
  • 4,722
  • 3
  • 37
  • 48

5 Answers5

14

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;
}
paulcam
  • 884
  • 5
  • 14
9

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.)

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • +1 Since you marked it as CW, I have updated it with other options (full qualification of the `enum` type is not required as the scope of the function declaration/definition is that of the class. – David Rodríguez - dribeas Mar 18 '11 at 22:22
3
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.

J T
  • 4,946
  • 5
  • 28
  • 38
  • Why the down vote? This code is solid. Code works beautifully in GCC 4.5.0 and msvc8/9 – J T Mar 18 '11 at 19:57
  • @JT: I didn't down vote. And it's my understanding this should be broken, as elaborated type-specifiers (I thought) are not valid for `enum`'s, but perhaps not. Either way, this won't fix his problem because, assuming it is valid, it's just equivalent to his own code: it's not needed. – GManNickG Mar 18 '11 at 19:57
  • @GMan, Is it okay if you elaborate why this isn't needed or valid, I have been writing code like J T's for years... (seemingly without problems) – Tom Mar 18 '11 at 20:03
  • @madmik3 @Tom: I've already made my position that it's invalid tentative; and upon further looking, I misunderstood something in the standard, so I reject my claim that it's invalid. *It's valid*. That said, all this would do is elaborate the *name* of `Myclass:encoding`; that has nothing to do with it's type or value. So it's just equivalent to what the questioner already has posted (just elaborated), so thusly doesn't fix anything. – GManNickG Mar 18 '11 at 20:12
  • Also, that makes this answer technically wrong, since you claim it's *forgotten* and therefore needed. Again, it's not. That said, I'm not the kind to down-vote answers unless they cause harm, so lucky you. – GManNickG Mar 18 '11 at 20:17
  • @Gman, yes my bad - I thought it was needed. Got an interview coming up and I got nervous that I had been doing enums wrong all this time... – Tom Mar 18 '11 at 20:24
  • @Tom: I agree with J T: the `enum` is not required. As a matter of fact, the qualification `Myclass::` is not needed either as the declaration is in the scope of the class and will find `encoding`, and the arguments of a member function are also looked up in the scope of the class where the member function belongs (contrary to the return type, that is looked up in the scope where the function is being defined. `Myclass::Myclass( encoding enc );` is fine, `encoding Myclass::foo() { return BINARY; };` would not be. – David Rodríguez - dribeas Mar 18 '11 at 22:30
2

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

littleadv
  • 20,100
  • 2
  • 36
  • 50
1

See this:

C++ pass enum as parameter

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

Myclass(encoding e);
Community
  • 1
  • 1
Brian Roach
  • 76,169
  • 12
  • 136
  • 161