私はいくつかのかなり単純な 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;
}
ありがとう。