2014-03-30 47 views
0

我正在嘗試編寫一個方法將unique_ptr從一個std :: vector轉移到另一個std :: vector。模板方法中迭代器參數的問題

template<typename T> 
void transferOne(vector<std::unique_ptr<T> > &to, 
       vector<std::unique_ptr<T> >::iterator what, 
       vector<std::unique_ptr<T> > &from) { 
    to.push_back(std::move(*what)); 
    from.erase(what); 
} 

鏘給我一個錯誤: 到依賴型名失蹤「類型名稱」之前「矢量> ::迭代」

任何想法如何應對呢?

回答

1

由於告訴你,把類型名在迭代器類型的面前:

template<typename T> 
void transferOne(vector<std::unique_ptr<T> > &to, 
       typename vector<std::unique_ptr<T> >::iterator what, 
       vector<std::unique_ptr<T> > &from) { 
    to.push_back(std::move(*what)); 
    from.erase(what); 
} 

關鍵字typename用來告訴編譯器,這vector<std::unique_ptr<T> >::iterator是一種類型。在沒有實例化模板的情況下,編譯器通常無法自行找出它,因爲可能存在vector模板的專門化,其中成員迭代器是一個靜態變量。

+0

謝謝。那很快。 – LRaiz