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

3

5 Answers 5

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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

7 Comments

Why the down vote? This code is solid. Code works beautifully in GCC 4.5.0 and msvc8/9
@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.
@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)
@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.
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.
|
2

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.