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:
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:
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
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:
Code:
#include <stack>
template <class T>
class A
{
public:
// constructor
A(const T &zz): z(zz) {}
T z;
// nested class
class B
{
public:
// constructor
B(const T &xx, const T &yy): x(xx), y(yy) {}
T x;
T y;
};
};
template <class T>
class Iter
{
public:
Iter(const T &xx): x(xx) {}
T x;
std::stack< A<T> > aStack;
std::stack< A<T>::B > bStack;
};
Code:
#include "nested.H"
int main(int argc, char *argv[])
{
A<int> myAObject(5);
A<int>::B myBOjbect(6,7);
Iter<int> myIterObject(2);
return 0;
}
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
Comment