template error ..

Started by
3 comments, last by y2jsave 13 years, 1 month ago
Hi ,
Whats the error in this code ..

#include<iostream>
#include<vector>
using namespace std;


template<typename TT1 , typename TT2>
class X
{
TT1<TT2> myContainer;
};


int main()
{
X<vector,int> cc;
}



compile errors are ::



temp2.cpp:9: error: 'TT1' is not a template
temp2.cpp: In function 'int main()':
temp2.cpp:15: error: type/value mismatch at argument 1 in template parameter list for 'template<class TT1, class TT2> class X'
temp2.cpp:15: error: expected a type, got 'vector'
temp2.cpp:15: error: invalid type in declaration before ';' token



..
y2jsave
Advertisement
You have two problems. The first is that in order to pass a template as a template parameter you need to specify that the parameter is a template template parameter. The second is that std::vector<> isn't a single argument template class; it's actually usually a two argument template class: one for the stored type and the second for the allocator.
template <template <typename T, typename A> class TT1, typename TT2>class X {  TT1<TT2, std::allocator<TT2> > myContainer; };
This means that instead of declaring TT1 as being a type, you should tell the compiler that it's a template (which is not a type on it's own, only an instance of a template is).

This should work, since it declares TT1 as being a templated class that accepts one parameter:
template<template <typename> class TT1 , typename TT2>class X{TT1<TT2> myContainer; };


*edit*
Uhh, too slow. Also SiCrane's explanation is better ;)
I wonder why you wouldn't just use:

#include<iostream>#include<vector>using namespace std;template<typename Container>class X{    Container myContainer;};int main(){    X<vector<int> > cc;}

You have two problems. The first is that in order to pass a template as a template parameter you need to specify that the parameter is a template template parameter. The second is that std::vector<> isn't a single argument template class; it's actually usually a two argument template class: one for the stored type and the second for the allocator.

template <template <typename T, typename A> class TT1, typename TT2>
class X {
TT1<TT2, std::allocator<TT2> > myContainer;
};



Hello ,
I understand your reply
does that mean that there is no way by which i can tell my template class that iam going to pass it a vector ( and not a vector<int> ) .


actually i want to do something like this .. using the same template

X<vector, int> myVect;
X<stack,char> myStack;

thanks in advance ..

regards
y2jsave

This topic is closed to new replies.

Advertisement