2012-04-07 19 views
3

はサンプルコードです:テンプレートテンプレート引数タイプ/値の不一致エラーここ

#include <stack> 
#include <cstddef> 

template <std::size_t N, 
     template <class> class Stack = std::stack 
     > 
class Tower : protected Stack<int> 
{ 
    public: 
     Tower() : Stack<int>(N) 
     { 
     } 
}; 


int main(int argc, char **argv) 
{ 
    Tower<5L> tower1(); 
} 

そして、私は、コンパイラ(GCC)を参照してください幸せではない:標準スタックコンテナがこのフォームを持っている

file.cpp: In function 'int main(int, char**)': 
file.cpp:18:11: error: type/value mismatch at argument 2 in template parameter 
list for 'template<long unsigned int N, template<class> class Stack> class Tower' 
file.cpp:18:11: error: expected a template of type 'template<class> class Stack', 
got 'template<class _Tp, class _Sequence> class std::stack' 
file.cpp:18:21: error: invalid type in declaration before ';' token 

template <class Type, class Container = deque<Type> > class stack;

意味ここでは、1つのテンプレート引数だけを渡すことをおすすめします。

これを解決する方法についてのご意見はありますか? ありがとう

答えて

4

'template<class> class Stack', got 'template<class _Tp, class _Sequence> class std::stack'がこの問題を示しています。ここで

は、あなたが2番目のパラメータがあります見ることができるようstd::stack

template< 
    class T, 
    class Container = std::deque<T> 
> class stack; 

のように見えるものです。

追加:

#include <deque> 
template <std::size_t N, 
     template <class T, class = std::deque<T>> class Stack = std::stack 
     > 

はそれをコンパイルするべきです。

4

std::stackには複数のテンプレート引数があります。したがって、あなたのケースでは使用できません。この問題を回避するには、テンプレートtypedefを使用してください。

template <typename T> 
using stack_with_one_type_parameter = std::stack<T>; 

template <std::size_t N, 
    template <class> class Stack = stack_with_one_type_parameter 
    > 
class Tower; 
2

ありがとうございました。ここで動作する私のコードの変更です:

#include <stack> 
#include <queue> 
#include <cstddef> 

template <std::size_t N, 
     class T, 
     template <class, class> class Stack = std::stack, 
     class C = std::deque<T> 
     > 
class Tower : protected Stack<T,C> 
{ 
    public: 
     Tower() : Stack<T,C>(N) 
     { 
     } 
}; 


int main(int argc, char **argv) 
{ 
    Tower<5UL, int> tower1(); 
    Tower<5UL, int, std::queue> tower2(); 
    Tower<5UL, int, std::stack, std::deque<int> > tower3(); 
} 
関連する問題