2013-10-17 51 views
-2

下面是我的代碼的精簡版,它給了我一個編譯器錯誤。編譯器告訴我把typename放在'std :: deque :: reverse_iterator'之前,這很有道理。但如果我這樣做,我會收到底部的錯誤。這是什麼意思?如何解決?type dependent in dependent scope

#include <iostream> 
#include <deque> 

template<class T> 
class Stack{ 

    public: 
     Stack(){} 
     ~Stack(){} 
     void push(T c) { s.push_back(c); } 
     void inspect() const{ 
      for(typename std::deque<T>::reverse_iterator i=s.rbegin(); i!=s.rend(); i++) 
       std::cout << *i << std::endl; 
     } 

    private: 
     typename std::deque<T> s; 
}; 


int main(){ 

    Stack<int> s; 
    s.push(1); 
    s.inspect(); 
    return 0; 
} 

錯誤:

error: no matching function for call to 'std::_Deque_iterator<int, int&, int*>::_Deque_iterator(std::reverse_iterator<std::_Deque_iterator<int, const int&, const int*> >::iterator_type)'| 
note: candidates are:| 
note: std::_Deque_iterator<_Tp, _Ref, _Ptr>::_Deque_iterator(const iterator&) [with _Tp = int; _Ref = int&; _Ptr = int*; std::_Deque_iterator<_Tp, _Ref, _Ptr>::iterator = std::_Deque_iterator<int, int&, int*>]| 
note: no known conversion for argument 1 from 'std::reverse_iterator<std::_Deque_iterator<int, const int&, const int*> >::iterator_type {aka std::_Deque_iterator<int, const int&, const int*>}' to 'const iterator& {aka const std::_Deque_iterator<int, int&, int*>&}'| 
note: std::_Deque_iterator<_Tp, _Ref, _Ptr>::_Deque_iterator() [with _Tp = int; _Ref = int&; _Ptr = int*]| 
note: candidate expects 0 arguments, 1 provided| 
note: std::_Deque_iterator<_Tp, _Ref, _Ptr>::_Deque_iterator(_Tp*, std::_Deque_iterator<_Tp, _Ref, _Ptr>::_Map_pointer) [with _Tp = int; _Ref = int&; _Ptr = int*; std::_Deque_iterator<_Tp, _Ref, _Ptr>::_Map_pointer = int**]| 
note: candidate expects 2 arguments, 1 provided| 

回答

1

這是一個很好的例子,其中使用auto將幫助您不僅僅是保存輸入。您處於const成員函數中,但嘗試使用您的數據成員的reverse_iterator而不是const_reverse_iterator

變化typename std::deque<T>::reverse_iterator變爲typename std::deque<T>::const_reverse_iterator,或者更簡單地,auto

這是對數據成員的額外typename的補充。

3

沒有什麼相關的約std::deque<T>的,所以不能是typename。只有在左邊依賴於模板參數的::右側的內容纔是相關的。

相關問題