簡體   English   中英

嘗試編譯包含二進制ifstream的代碼時,編譯會導致錯誤

[英]Compilation results in an error when trying to compile code containing binary ifstream

我通過輸入文件流類(ifstream)訪問二進制文件時遇到問題。

我的方法從以下調用函數開始:

void ReadFile(vector<string>& argv, ostream& oss){
   string FileName = argv.at(2) + "INPUT" ;
   ifstream BinFile ;
   OpenBinaryFile(FileName, BinFile) ;
   return ;
}

調用的函數如下所示:

void OpenBinaryFile(string& FileName, ifstream& BinFile){
   using namespace std ;
   BinFile(FileName.c_str(),ifstream::binary | ifstream::in) ;
}

當我嘗試使用gcc版本4.9.2編譯此簡單方案時,出現以下錯誤:

error: no match for call to ‘(std::ifstream {aka std::basic_ifstream<char>}) (const char*, std::_Ios_Openmode)’
BinFile(FileName.c_str(),ifstream::binary | ifstream::in) ;
                                                        ^

我試圖將插入符號(“ ^”)放置在編譯器的確切位置。

這里發生了什么? 我感到困惑。

謝謝!

有兩種打開流的方法。

  1. 施工期間,在聲明中:

     std::ifstream BinFile(filename, std::ifstream::binary | std::ifstream::in); 
  2. 構造后,使用std::ifstream::open函數:

     std::ifstream BinFile; BinFile.open(filename, std::ifstream::binary | std::ifstream::in); 

在您的問題中,您嘗試將兩者混為一談。 這導致嘗試調用對象BinFile上不存在的“函數調用運算符” operator()

如所寫,您正在使用構造在調用例程堆棧上的對象來調用構造函數。 請參閱http://www.cplusplus.com/reference/fstream/ifstream/ifstream/中記錄的構造函數

暫無
暫無

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

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