【发布时间】:2013-11-02 17:02:05
【问题描述】:
我正在使用一些相当简单的 C++ 代码将文件的内容转入std::string:
// Read contents of file given to a std::string.
std::string f = "main.js";
std::ifstream ifs(f.c_str());
std::stringstream sstr;
sstr << ifs.rdbuf();
std::string code = sstr.str();
但是当我编译时,我得到了这个错误:
error: could not convert ‘((*(const std::allocator<char>*)(& std::allocator<char>())),
(operator new(4u), (<statement>, ((std::string*)<anonymous>))))’ from ‘std::string*
{aka std::basic_string<char>*}’ to ‘std::string {aka std::basic_string<char>}’
我知道这可能是一个简单的错误,但我仍在学习 C++。应该只是一个简单的类型混淆之类的。
根据要求,这是我正在尝试做的一些示例代码:
std::string Slurp(std::string f)
{
std::ifstream ifs(f.c_str());
std::stringstream sstr;
sstr << ifs.rdbuf();
std::string code = sstr.str();
return code;
}
谢谢。
【问题讨论】:
标签: c++ file scripting ifstream sstream