我想定義一個構造函數,它與任何具有begin,end,operator ++的構造函數一起工作。 換句話說,我想這種行爲(評論說,工作代碼):模板構造函數和typedef迭代器
/*Polyn(std::vector<double> &a) : CalcDerivative(0) , CalcIntegral(0) {
for(std::vector<double>::iterator i = a.begin();i < a.end();++i)
params.push_back(*i);
}*/
與其他迭代器。 (例如列表)。
template <typename T>
Polyn(const T &a) : CalcDerivative(0) , CalcIntegral(0) {
typename std::vector<T>::iterator iter;
for(iter i = a.begin();i < a.end();++i) //LINEA 18!!
params.push_back(*i);
}
我所得到的是這樣的編譯錯誤:
polyn.h: In constructor ‘Polyn::Polyn(const T&)’:
polyn.h:18: error: expected ‘;’ before ‘i’
爲什麼呢?如何解決我的代碼?
你忘了「typedef」嗎? – 2012-03-04 17:13:22
另外請注意,你要麼傳遞'std :: vector'或使用'typename T :: const_iterator'(實際上你需要'const_iterator'在這兩個cass。 –
2012-03-04 17:19:21
謝謝奧利查爾斯沃斯,我混淆了typename與typedef,對不起我的newbyness – jimifiki 2012-03-04 17:26:10