簡體   English   中英

非常基本的C ++錯誤

[英]Very basic C++ error

大家好,我正在寫有史以來最簡單的事情,只是創建了一個ifstream來讀取文本文件,並且出現了一個奇怪的錯誤。 這是代碼(請注意:iostream和fstream的'<'很好地寫在了我的代碼中,但是我不能在這里寫出來)

#include "genlib.h"
#include "simpio.h"
#include "random.h"
#include "vector.h"
#include "map.h"
#include <iostream>
#include <fstream>

int main() {
 ifstream in;
 in.open("Hamlet.txt");
 if (in.fail()) Error("Could not open file");
 return 0;
}

在ifstream輸入后,出現以下錯誤; 行:“錯誤:'='令牌之前的預期不合格ID”

知道出什么事了嗎?

謝謝

ifstream in;過去的唯一不尋常的東西ifstream in; Error調用。 我的瘋狂猜測是,這是一個寫得不好的宏。 嘗試以下方法:

int main() {
 ifstream in;
 in.open("Hamlet.txt");
 if (in.fail()) { Error("Could not open file"); }
 return 0;
}

請注意Error周圍的新括號。

您需要添加

using namespace std;

無條件使用庫中的任意名稱。 否則,聲明必須為

std::ifstream in;

也有選擇

using std::ifstream;

但我不建議這樣做,因為您可能不會經常寫出std::ifstream

我的猜測是,在您自己的一個包含文件中(“ genlib.h”和“ simpio.h”似乎是非標准的),您被#defined定義為“ in”

嘗試直接在構造函數中打開文件:

ifstream inf ( "Hamlet.txt" , ifstream::in );

使用std :: ifstream

(並提供可編譯的代碼;這些不是全部標准標頭,什么是Error()?)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM