compile error with inline/non-inline function
I have a class with an enum and a simple method like this;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class MyClass
{
enum Code { A, H, M, Q, S};
Code foo( char ch)
{
switch (ch)
{
case 'A': return A;
case 'M': return M;
case 'Q': return Q;
case 'S': return S;
default : return H;
}
}
public:
MyClass(){}
};
|
If I define
inside the class, then everything seems OK. At least, I can compile the code normally. But if I move the body of the function outside of the class,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
class MyClass
{
enum Code { A, H, M, Q, S};
Code foo( char ch);
public:
MyClass(){}
};
Code MyClass::foo( char ch )
{
switch (ch)
{
case 'A': return A;
case 'M': return M;
case 'Q': return Q;
case 'S': return S;
default : return H;
}
}
|
I'll get a bunch of nonsense compile errors like "missing ; before...", etc.
So does anyone know why this happens?
It's your code that is nonsense (as far as the compiler is concerned)
Remember that classes in C++ are also scopes.
Here declaration of Code enum has MyClass scope:
1 2 3 4
|
class MyClass
{
enum Code { A, H, M, Q, S};
};
|
Use scope resolution operator:
1 2 3 4 5 6 7 8 9 10 11
|
MyClass::Code MyClass::foo( char ch )
{
switch (ch)
{
case 'A': return A;
case 'M': return M;
case 'Q': return Q;
case 'S': return S;
default : return H;
}
}
|
Ok, thanks, Boolivar!
because MyClass::foo can not see the Code
Topic archived. No new replies allowed.