【发布时间】:2017-05-21 18:58:41
【问题描述】:
我正在尝试为 linux/MacOS 转换用 C++14 编写的应用程序。它使用 boost::filesystem,但不适用于某些 iostream 操作。例如:
boost::filesystem::path file = name;
std::ifstream fin(file.c_str());
此代码无法在 Windows 10 上使用 MinGW 和 GCC 6.3 进行编译:
错误:没有匹配的调用函数 'std::basic_ifstream::basic_ifstream(const value_type*)' std::ifstream fin(file.c_str());
我想如果我可以将 std::ifstream 转换为 boost::filesystem::ifstream 我可以让它工作......所以我将代码更改为:
boost::filesystem::path file = name;
boost::filesystem::ifstream fin(file.c_str());
if (!fin)
{
file = pathToAppData / "files/expansion/assets/resources/basestation/config/mapFiles/racing" / name;
fin = boost::filesystem::ifstream(file.c_str());
if (!fin)
throw std::runtime_error(std::string("Cannot open Anki Overdrive map file ") + file.string() + ".");
}
fin >> (*this);
这导致了这个错误:
错误:'const boost::filesystem::basic_ifstream& boost::filesystem::basic_ifstream::operator=(const boost::filesystem::basic_ifstream&) [with charT = char; traits = std::char_traits]' 在此上下文中是私有的
fin = boost::filesystem::ifstream(file.c_str());
看起来我无法重新分配 boost::filesystem::ifstream 一旦创建...我能够将该行更改为以下内容并编译,但我想知道这是否是正确的方法这样做:
boost::filesystem::ifstream fin(file.c_str());
额外问题:一旦我让它工作,这段代码是否也能在 linux 上工作?
【问题讨论】:
-
什么是
name?你能把它传递给ifstream构造函数吗?