Enum in C++: how to pass as parameter?
Solution 1
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;
}
Solution 2
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.)
Solution 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.
Solution 4
Remove the static
. Generally, mentioning the exact error will help you get better answers.
Solution 5
See this:
You reference it differently depending on the scope. In your own class you say
Myclass(encoding e);
Related videos on Youtube

polemon
OH GOD HOW DID IT GET HERE, I AM NOT GOOD WITH COMPUTER! echo "[email protected]" | tr '[a-z]' '[n-za-m]'
Updated on March 19, 2020Comments
-
polemon almost 3 years
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)?
-
pmr almost 12 yearsWhat is
static class
trying to achieve?
-
-
J T almost 12 yearsWhy the down vote? This code is solid. Code works beautifully in GCC 4.5.0 and msvc8/9
-
GManNickG almost 12 years@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. -
Tom almost 12 years@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)
-
GManNickG almost 12 years@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 almost 12 yearsAlso, 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.
-
Tom almost 12 years@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...
-
David Rodríguez - dribeas almost 12 years+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 almost 12 years@Tom: I agree with J T: the
enum
is not required. As a matter of fact, the qualificationMyclass::
is not needed either as the declaration is in the scope of the class and will findencoding
, 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.