在Microsoft中起作用的Borland错误:找不到"ifstream::open"的匹配项

Borland error that works in Microsoft: Couldn't find a match for 'ifstream::open'

本文关键字:ifstream open 起作用 Borland 错误 Microsoft 找不到      更新时间:2023-10-16

我想读取一个名称为:abc.txt的文本文件

文本文件只包含简单的a、b和c,它们分别在各自的行上。

当我使用Microsoft编译器编译时,它完全没有问题,我得到了我期望的输出(见下文):

a
b
c
(blank line here)

下面是我使用的Borland编译行:

bcc32 -w -ebor.exe main.cpp

这是我使用的main.cpp文件:

main.cpp

#include <iostream>
#include <fstream>
#include <string>
void printout(const std::string &file);
int main(void)
{
  printout("abc.txt");
  return 0;
}
void printout(const std::string &file)
{
  std::ifstream words;
  std::string   str;
  words.open(file);
  while(!words.eof())
  {
    std::getline(words, str);
    std::cout << str << "n";
  }
  words.close();
}

我在Borland中得到的确切错误如下:

错误E2285 main.cpp 17:无法在函数printout(const std::string &)中找到'ifstream::open(const std::string)'的匹配

我也得到了一个警告,但似乎它发生的唯一原因是由于错误阻止'file'被使用:

警告W8057 main.cpp 26:函数printout(const std::string &)中从未使用参数'file'

在c++ 11之前,std::ifstream::open需要const char *。使用这个。

words.open( file.c_str() );