尝试编译包含二进制 ifstream 的代码时编译导致错误

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

我 运行 遇到了通过输入文件流 class (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.

上调用不存在的 "function call operator" operator()

正如所写的那样,您正在使用已经在调用例程的堆栈上构造的对象调用构造函数。请参阅 http://www.cplusplus.com/reference/fstream/ifstream/ifstream/

中记录的构造函数