我是 C 的初學者。呼叫期望的函式的正確方法是什么std::istream&?
用 嘗試過read(std::cin);,但我從編譯器那里得到一個錯誤。
typedef double Element;
template<typename T>
std::list<T> read(std::istream& i) {
Element input;
std::list<Element> l;
while(i>>input) {
l.push_back(input);
}
return l;
}
uj5u.com熱心網友回復:
這與std::istream&引數無關。
問題在于該函式是一個函式模板,它需要一個明確的模板引數來確定應該從流中讀取的型別,例如:
read<int>(std::cin)
來自編譯器的錯誤訊息也應該告訴你類似的事情。
除此之外,您沒有T在函式中使用。可能您想替換所有使用的ElementbyT并洗掉typedef.
uj5u.com熱心網友回復:
你只有一個小的語法錯誤:
試試這個代碼:
typedef double Element;
class test{
public:
auto read(std::istream& i){
Element input;
std::list<Element> l;
while(i>>input){
l.push_back(input);
}
return l;
}
};
int main(){
test t;
t.read(std::cin);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394042.html
上一篇:如何獲取串列中相同值的索引?
下一篇:只有特殊字符從串列中洗掉
