我正在考虑实现slurp
,这是一个读取文件并返回字符串的函数:
在main()
函数中使用此代码时,它可以正常工作:
int main(){
std::ifstream ifs("stuff.txt");
std::string content((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
std::cout << content;
return 0;
}
但是,当我将文件移出到另一个文件中的函数时:
std::string slurp(std::string filename){
std::ifstream ifs(filename);
std::string content((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
return content;
}
编译时出现这样的错误:
[ 33%] Building CXX object CMakeFiles/image_calibration.dir/source/cpp/kanya.cpp.o
/Users/chris/Development/chit/image-bench/source/cpp/kanya.cpp:15:19: error: implicit instantiation of undefined template 'std::__1::basic_ifstream<char,
std::__1::char_traits<char> >'
std::ifstream ifs(filename);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iosfwd:131:32: note: template is declared here
class _LIBCPP_TEMPLATE_VIS basic_ifstream;
发生了什么事?
答案 0 :(得分:0)
可能你刚刚失踪:
#include <fstream>