Question

I have two classes, which I shall call classA and classB.

In classA I have some types defined, such as T5.
In classB I use such types defined in classA.

In order to avoid writing

typename classA<T1,T2>::T5

throughout my code, I want to create a class indipendent type, which I would be able to call directly by simply its name - templatedClassA in the example. The template paremeters would still be parametric, and I would define them in future projects using classA and classB.

#include <cstdlib>
#include <iostream>

using namespace std;

//classA
template <class T1, class T2>
class classA
{
 public:  
 typedef T1 T5;
 classA();
};

template <class T1, class T2>
classA<T1,T2>::classA(){}
//end classA

template <class T1, class T2>
typedef classA<T1,T2> templatedClassA;

template <class T1, class T2>
typedef typename classA<T1,T2>::T5 T6;






//classB
template <class T3, class T4>
class classB
{
 public:
 classB();
};

template <class T3, class T4>
classB<T3,T4>::classB(){}
//end classB



int main(int argc, char *argv[])
{

    classB<int, double> classBInstance;

    system("PAUSE");
    return EXIT_SUCCESS;
}

When compiling the above code, I get the following two errors:

template declaration of `typedef class classA<T1, T2> templatedClassA'
template declaration of `typedef typename classA<T1, T2>::T5 T6'.

What am I doing wrong?

Was it helpful?

Solution

Templates cannot be used in typedef declarations. You have to wrap the declaration in a class like this:

template <class T1, class T2>
class templatedClassA {
    typedef classA<T1, T2> type;
};

template <class T1, class T2>
class T6 {
   typedef typename classA<T1, T2>::T5 type;
};

In C++11 you can use the using alias as shown in the other answer.

OTHER TIPS

In C++11, you may do:

template <class T1, class T2>
using templatedClassA = classA<T1, T2>;

template <class T1, class T2>
using T6 = typename classA<T1, T2>::T5;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top