Enumeration in C ++: how to pass parameter?

I have the following enumeration in my class definition:

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

Then in the definition of the method:

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

This does not work, but what am I doing wrong? How to pass an enumeration member that is defined inside a class for member methods (and other methods)?

+3
source share
5 answers

I'm not quite sure why you are using a "static class" here. This template is completely suitable for me in VS2010:

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

CFoo::CFoo(Bar barIn)
{
    barIn;
}
+7
source

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 the errors you get so that we can solve real problems, not fake ones. (Give us a * compiled * example that reproduces your problem.)

+6
source

static. , .

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

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

Just remember the enum keyword in the parameter.

+1
source

See this:

C ++ pass enum as parameter

You refer to it differently depending on the scope. In your class, you say

Myclass(encoding e);
0
source

All Articles