0
我想從數據文件中讀取值。除非得到lol.GetNextItem(x);否則所有東西都編譯好。它表示x是未聲明的標識符並且未定義。我不確定如何解決它。我相信我附上了解決這個問題所需的部分代碼,但如果需要其他代碼,請告訴我。C++未定義的變量,而在數據中讀取
class SortedList
{
private:
int length;
ItemType values[MAX_ITEMS];
int currentPos;
public:
void GetNextItem(ItemType &x);
}
void SortedList:: GetNextItem(ItemType &x)
{
currentPos++;
}
int main()
{
ifstream bug;
int i = 0;
int size = 0;
bug.open("num.dat");
float values[10];
while (!bug.eof())
{
bug >> values[i];
i++;
size++;
}
SortedList lol;
lol.GetNextItem(x);
bug.close();
return 0;
}
這是您的整個代碼?正如我所看到的,你沒有在這段代碼中聲明變量'x',所以編譯器只是抱怨它 – PepeHands
,最終會產生什麼? – Marco
爲了解決另一個問題,請看看[爲什麼iostream :: eof在循環條件內被認爲是錯誤的?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-循環條件考慮錯誤) –