gpt4 book ai didi

c++ - 未定义模板的隐式实例化 : basic_fstream

转载 作者:行者123 更新时间:2023-11-30 05:22:01 25 4
gpt4 key购买 nike

我正在使用 clang/llvm 8.0.0 在 macOS 上编译它。为 C++14 编译。

#include <fstream>
#include <string>

using namespace std;

basic_fstream<string> open_file(string file);

int main()
{
basic_fstream<string> f = open_file("test");
f.close();
}

basic_fstream<string> open_file(string file) {
basic_fstream<string> f;
f.open(file);

if(!f.is_open()) {
f.clear();
f.open(file, ios_base::out);
f.close();
f.open(file);
}

return f;
}

它会产生一个很长的错误列表,但第一个是:

implicit instantiation of undefined template 'std::__1::codecvt<std::__1::basic_string<char>, char, __mbstate_t>'
__always_noconv_ = __cv_->always_noconv();

最佳答案

basic_fstream 的模板参数是字符类型,不是字符串类型。 C++ 实现支持的典型字符类型为 basic_fstream<char>basic_fstream<wchar_t> ,例如。

但是为什么要使用这个模板呢?只需使用 std::fstream ,又名 std::basic_fstream<char> ;或 std::wfstream ,又名 std::basic_fstream<wchar_t> .

它也不会伤害到 get rid of using namespace std; while your at it ,也是。

关于c++ - 未定义模板的隐式实例化 : basic_fstream<string>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39864032/

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