Compiler error with nested templates (simple example included)

D

David Williams

Hi all, I get errors when compiling the .cpp file below, both on Visual
Studio and G++.

01: //-------------------------------------------------
02: template <typename Type>
03: class Outer
04: {
05: public:
06: Outer outerFunc(void);
07:
08: class Inner
09: {
10: public:
11: Outer innerFuncWorks(void);
12: Inner innerFuncBroke(void);
13: };
14: };
15:
16: template <typename Type>
17: Outer<Type> Outer<Type>::eek:uterFunc()
18: {
19: }
20:
21: template <typename Type>
22: Outer<Type> Outer<Type>::Inner::innerFuncWorks()
21: {
23: }
24:
25: template <typename Type>
26: Outer<Type>::Inner<Type> Outer<Type>::Inner::innerFuncBroke()
27: {
28: }
29:
30: int main()
31: {
32: return 0;
33: }
34: //-------------------------------------------------

For example, G++ says:

Compiling: main.cpp
main.cpp:26: error: non-template type `Inner' used as a template
main.cpp:27: error: ISO C++ forbids declaration of `innerFuncBroke' with
no type
main.cpp:27: error: prototype for `int
Outer<Type>::Inner::innerFuncBroke()' does not match any in class
`Outer<Type>::Inner'
main.cpp:11: error: candidate is: Outer<Type>::Inner
Outer<Type>::Inner::innerFuncBroke()
main.cpp:27: error: template definition of non-template `int
Outer<Type>::Inner::innerFuncBroke()'
main.cpp: In member function `int Outer<Type>::Inner::innerFuncBroke()':
main.cpp:28: warning: no return statement in function returning non-void
Process terminated with status 1 (0 minutes, 0 seconds)
5 errors, 1 warnings

Essentially it appears to complain about the line where 'innerFuncBroke'
is defined. The only difference between this and 'innerFuncWorks' is the
return type, so this is presumably where the problem lies. I'm aware
that i'm returning no value and the compiler warns about that but it
doesn't appear to be the problem and i'm trying to keep the example simple.

Any ideas appriciated!

David
 
G

Gianni Mariani

David said:
Hi all, I get errors when compiling the .cpp file below, both on Visual
Studio and G++.
.....
24:
25: template <typename Type>
26: Outer<Type>::Inner<Type> Outer<Type>::Inner::innerFuncBroke()

I suspect you need a "typename"
i.e.
typename Outer said:
....
 
D

David Williams

Gianni said:
I suspect you need a "typename"
i.e.


...

Thank you! I also needed to remove the '<type>' in 'Inner<Type>' but I
had already tried various combinations of that. Adding typename fixed it.

Also, thanks for such a quick reply!
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top