我希望能夠創建一個函數,在該函數中指定一個參數以同時爲該容器提供模板化容器和模板化元素類型。這可能嗎?我收到「錯誤C2988:不可模仿的模板聲明/定義」等等。這是有問題的功能。C++模板 - 指定容器類型和它容納的容器元素類型
template<class Iter, class Elem>
void readIntoP(Iter<Elem> aCont){
ifstream ifss("data.dat");
string aString;
int counter = 0;
item tempItem;
while(ifss >> aString){
istringstream iss(aString);
if(counter == 0){
tempItem.name = aString;
}else if(counter == 1){
int aNum = 0;
iss >> aNum;
tempItem.iid = aNum;
}else{
double aNum = 0;
iss >> aNum;
tempItem.value = aNum;
aCont.push_back(tempItem);
counter = -1;
}
++counter;
}
}
重複排序-的[C++函數模板編譯錯誤「‘containerType’不是一個模板」 ](http://stackoverflow.com/questions/3436518/c-function-template-compiles-error-containertype-is-not-a-template)(解決方案至少是完全相同的)。 – 2010-08-11 00:03:03