By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,083 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,083 IT Pros & Developers. It's quick & easy.

Templated classes with nested classes

card
P: 10
Hi everyone,

I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes:
Expand|Select|Wrap|Line Numbers
  1. #include <stack>
  2.  
  3. template <class T>
  4. class A
  5. {
  6. public:
  7.  
  8.     // constructor
  9.     A(const T &zz): z(zz) {}
  10.     T z;
  11.  
  12.     // nested class
  13.     class B
  14.     {
  15.     public:
  16.         // constructor
  17.         B(const T &xx, const T &yy): x(xx), y(yy) {}
  18.         T x;
  19.         T y;
  20.     };
  21. };
  22.  
  23. template <class T>
  24. class Iter
  25. {
  26. public:
  27.     Iter(const T &xx): x(xx) {}
  28.     T x;
  29.  
  30.     std::stack< A<T> > aStack;
  31.     std::stack< A<T>::B > bStack;
  32. };
  33.  
  34.  
As you can see, class A is a templated class and class B is nested within A. I have another templated class called Iter. Within Iter, I have two stacks. One contains objects of A and the other objects of the nested class B contained in A. A simple main program to build the code is below:

Expand|Select|Wrap|Line Numbers
  1. #include "nested.H"
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5.     A<int> myAObject(5);
  6.     A<int>::B myBOjbect(6,7);
  7.     Iter<int> myIterObject(2);
  8.  
  9.     return 0;
  10. }
  11.  
The build with g++ fails with the following error:

nested.H:44: error: type/value mismatch at argument 1 in template parameter list for ‘template<cla\
ss _Tp, class _Sequence> class std::stack’
nested.H:44: error: expected a type, got ‘A<T>::B’
nested.H:44: error: template argument 2 is invalid

As you can see, it doesn't know what to do with A<T>::B in the stack declaration of the templated class Iter. How do I reference this nested class within Iter? Obviously, I can do it in main as shown above. Many thanks.

-David
Oct 19 '08 #1
Share this Question
Share on Google+
2 Replies


boxfish
Expert 100+
P: 469
Try changing
std::stack< A<T>::B > bStack;
to
std::stack< typename A<T>::B > bStack;
Somehow that lets the compiler know A<T>::B is a type, but I have no idea why you have to do it. Template syntax-
AAAAAAAAAAAAARRRRRRRRRRGGGH!!
Good luck.
Oct 19 '08 #2

card
P: 10
Try changing
std::stack< A<T>::B > bStack;
to
std::stack< typename A<T>::B > bStack;
Somehow that lets the compiler know A<T>::B is a type, but I have no idea why you have to do it. Template syntax-
AAAAAAAAAAAAARRRRRRRRRRGGGH!!
Good luck.
Thanks boxfish! I've been pulling my hair out for a couple hours. That did the trick.
Oct 19 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.