Enum in derived template class

B

Barzo

Hi,
I've two problems on compiling the following example with Eclipse and
GCC.


1 namespace MyNamespace
2 {
3 template<class T>
4 class ClassA
5 {
6 public:
7 typedef enum EnumA {A, B, C};
8
9 void Method(EnumA value);
10
11 static MyOtherClass::ReturnValues StaticMember()
12 {
13 //..do something..
14 };
15 }
16
17 template<class T>
18 class ClassB : ClassA<T>
19 {
20 public:
21 void Method(EnumA value);
22 }
23
24 }


At line 11 I get:
"type 'MyOtherClass' is not derived from type
'MyNamespace::ClassA<T>'"

At line 21 I get:
"'EnumA' has not been declared"

For the first error I don't have idea how to fix, MyOtherClass a class
defined!
For the second I think I have to specify the entire name:

MyNamespace::ClassA<T>::EnumA

but is there a less verbose method?!
Could someone give me some suggestions?

Thanks!
Daniele.
 
A

Alf P. Steinbach

* Barzo:
Hi,
I've two problems on compiling the following example with Eclipse and
GCC.


1 namespace MyNamespace
2 {
3 template<class T>
4 class ClassA
5 {
6 public:
7 typedef enum EnumA {A, B, C};
8
9 void Method(EnumA value);
10
11 static MyOtherClass::ReturnValues StaticMember()
12 {
13 //..do something..
14 };
15 }
16
17 template<class T>
18 class ClassB : ClassA<T>
19 {
20 public:
21 void Method(EnumA value);
22 }
23
24 }

<code>
class MyOtherClass
{
public:
enum ReturnValues{ x, y, z };
};

namespace MyNamespace
{
template<class T>
class ClassA
{
public:
enum EnumA {a, b, c};

void Method(EnumA value);

static MyOtherClass::ReturnValues StaticMember()
{
//..do something..
return MyOtherClass::x;
}
};

template<class T>
class ClassB : ClassA<T>
{
public:
typedef typename ClassA<T>::EnumA EnumA;

void Method(EnumA value);
};
}

int main()
{}
</code>



Note the difference wrt. semicolons and so on.

And by the way, don't use all uppercase for ordinary names (even though that is
a common convention for constants in Java).

Because in C++ that conflicts with the convention for macros, which is much more
important.



Cheers & hth.,

- Alf
 
A

Alexander Bartolich

Barzo said:
Thanks a lot Alexander!
The FAQ explains how to declare a member, but how to declare a member
parameter (which is my case) like these:

void Method1(EnumA value);
void Method2(EnumA value = B);

It's the same problem: you need to specify keyword "typename".
For example

void Method1(typename ClassA<T>::EnumA value);
void Method2(typename ClassA<T>::EnumA value = ClassA<T>::A);

You can simplify the syntax a bit defining a nested type:
typedef typename ClassA<T>::EnumA EnumA;
void Method1(EnumA value);
void Method2(EnumA value = ClassA<T>::A);

But again keyword "typename" is required.
 
B

Barzo

You can simplify the syntax a bit defining a nested type:
  typedef typename ClassA<T>::EnumA EnumA;
  void Method1(EnumA value);
  void Method2(EnumA value = ClassA<T>::A);

But again keyword "typename" is required.

Thanks a lot Alexander,
now it compile.

Daniele.
 
B

Barzo

<code>
...
</code>

Note the difference wrt. semicolons and so on.

And by the way, don't use all uppercase for ordinary names (even though that is
a common convention for constants in Java).

Because in C++ that conflicts with the convention for macros, which is much more
important.

Thanks for your suggestions Alf.
I've modified the enum name with E*** and types name with T***

Daniele.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top