![](/img/trans.png)
[英]Including const in operator overloading argument list gives error (C++)
[英]Why does >> operator gives error on const file in C++?
我有這段代碼:
void NeighborsList::insertVertexes(const ifstream & inputFile)
{
int tempS, tempT;
for (int i = 0; i < numOfVertexes; i++)
{
inputFile >> tempS;
inputFile >> tempT;
addEdge(tempS, tempT);
}
}
我正在嘗試獲取文件的輸入。 一旦我刪除了函數參數中的 const - (ifstream & inputFile) 它就可以工作了。
給定一個const
對象或引用,只能執行const
操作。 std::istream::operator>>
不是const
操作,因此它不能在這里使用。
std::istream::operator>>
不是const
操作是有道理的,因為它改變了流的可觀察狀態。 例如,文件上的讀取位置以及狀態指示器(如fail
和eof
已更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.