gpt4 book ai didi

c++ - 适用于 Microsoft : Couldn't find a match for 'ifstream::open' 的 Borland 错误

转载 作者:太空狗 更新时间:2023-10-29 21:02:15 25 4
gpt4 key购买 nike

我想读入一个名为: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:函数打印输出中从未使用参数"file"(const std::string &)

非常感谢任何帮助,谢谢。

最佳答案

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

words.open( file.c_str() );

关于c++ - 适用于 Microsoft : Couldn't find a match for 'ifstream::open' 的 Borland 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15889762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com