2011-10-31 116 views
0

我嘗試編譯下面的代碼示例「C++模板 - 完全指南」由大衛Vandevoorde和尼古拉M.約祖蒂斯:模板模板參數 - 編譯出錯

#include <iostream> 
    #include <deque> 
    #include <vector> 
    #include <stdexcept> 
    #include <memory> 

    template < typename T, 
       template < typename ELEM, typename = std::allocator<ELEM> > 
          class CONT = std::deque > 
    class ourstack 
    { 
    private: 
     CONT<T> elems; 
    public: 
     void push(T const&); 
     void pop(); 
     T top() const; 
     bool empty() const 
     { return elems.empty(); } 
     template < typename T2 > 
       template < typename ELEM2, 
          typename = std::allocator<ELEM2> > class CONT2 > 
     ourstack< T, CONT >& operator = (ourstack< T2, CONT2 > const &); 
    }; 

    template < typename T, 
       template < typename, typename > class CONT > 
    void ourstack< T, CONT>::push(T const& elem) 
    { 
     elems.push_back(elem); 
    } 

    template < typename T, 
       template < typename, typename > class CONT > 
    void ourstack< T, CONT>::pop() 
    { 
     if (elems.empty()) 
     { 
      throw std::out_of_range("Stack empty"); 
     } 
     elems.pop_back(); 
    } 

    template < typename T, 
       template < typename, typename > class CONT > 
    void ourstack< T, CONT>::top() const 
    { 
     if (elems.empty()) 
     { 
      throw std::out_of_range("Stack empty"); 
     } 
     return elems.back(); 
    } 

    template < typename T, 
       template < typename, typename > class CONT > 
     template < typename T2 > 
        template < typename, typename > class CONT2 > 
    ourstack< T, CONT >& ourstack< T, CONT >::operator = (ourstack< T2, CONT2 > const & op2 ) 
    { 
     if ((void*) this == (void*) op2) 
     { 
      return *this; 
     } 
     ourstack<T2> tmp(op2); 
     elems.clear(); 
     while (!tmp.empty()) 
     { 
      elems.push_front(tmp.top()); 
      tmp.pop(); 
     } 
     return *this; 
    } 

    int main(int argc, char* argv[]) 
    { 
     ourstack<int> s; 
     return 0; 
    } 

我使用gcc版本4.4.3。

編譯器寫郵件:

g++ -Wall template_of_template.cpp -o template_of_template 
    template_of_template.cpp:22: error: too many template-parameter-lists 
template_of_template.cpp:22: error: expected unqualified-id before ‘>’ token 
template_of_template.cpp:46: error: prototype for ‘void ourstack<T, CONT>::top() const’ does not match any in class ‘ourstack<T, CONT>’ 
template_of_template.cpp:17: error: candidate is: T ourstack<T, CONT>::top() const 
template_of_template.cpp:58: error: too many template-parameter-lists 
template_of_template.cpp:58: error: expected unqualified-id before ‘>’ token 

什麼問題?

+2

同時使用'outstack'和'ourstack'。 .. –

回答

3

的代碼唯一的問題是,你需要學習如何更準確地打字。 ; - ]

固定多個錯別字後(t代替r>代替,),這裏是在編譯狀態相同的代碼:

#include <iostream> 
#include <deque> 
#include <vector> 
#include <stdexcept> 
#include <memory> 

template < typename T, 
      template < typename ELEM, 
         typename = std::allocator<ELEM> > class CONT = std::deque > 
class ourstack 
{ 
private: 
    CONT<T> elems; 
public: 
    void push(T const&); 
    void pop(); 
    T top() const; 

    bool empty() const 
    { return elems.empty(); } 

    template < typename T2, 
       template < typename ELEM2, 
          typename = std::allocator<ELEM2> > class CONT2 > 
    ourstack< T, CONT >& operator = (ourstack< T2, CONT2 > const &); 
}; 

template < typename T, 
      template < typename, typename > class CONT > 
void ourstack< T, CONT>::push(T const& elem) 
{ 
    elems.push_back(elem); 
} 

template < typename T, 
      template < typename, typename > class CONT > 
void ourstack< T, CONT>::pop() 
{ 
    if (elems.empty()) 
    { 
     throw std::out_of_range("Stack empty"); 
    } 
    elems.pop_back(); 
} 

template < typename T, 
      template < typename, typename > class CONT > 
T ourstack< T, CONT>::top() const 
{ 
    if (elems.empty()) 
    { 
     throw std::out_of_range("Stack empty"); 
    } 
    return elems.back(); 
} 

template < typename T, 
      template < typename, typename > class CONT > 
template < typename T2, 
      template < typename, typename > class CONT2 > 
ourstack< T, CONT >& ourstack< T, CONT >::operator = (ourstack< T2, CONT2 > const & op2 ) 
{ 
    if ((void*) this == (void*) op2) 
    { 
     return *this; 
    } 
    ourstack<T2> tmp(op2); 
    elems.clear(); 
    while (!tmp.empty()) 
    { 
     elems.push_front(tmp.top()); 
     tmp.pop(); 
    } 
    return *this; 
} 

int main(int argc, char* argv[]) 
{ 
    ourstack<int> s; 
    return 0; 
} 
1
template < typename T2 > 
      template < typename ELEM2, 
         typename = std::allocator<ELEM2> > class CONT2 > 
    ourstack< T, CONT >& operator = (ourstack< T2, CONT2 > const &); 

那裏有兩個template聲明,那裏應該只有一個。我不知道,但我想你想要的是這樣的:

template < 
     typename T2 
     , template < typename ELEM2, typename = std::allocator<ELEM2> > class CONT2 
    > 
    ourstack< T, CONT >& operator = (ourstack< T2, CONT2 > const &); 

另外請注意,提供模板模板參數的名稱是沒有意義的。