简体   繁体   中英

implicit instantiation of undefined template: basic_fstream<string>

I'm compiling this on macOS using clang/llvm 8.0.0. Compiling for 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;
}

It produces a long error list but the first is:

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

The template parameter to basic_fstream is a character type, not a string type. The typical character types that are supported by a C++ implementation would be basic_fstream<char> and basic_fstream<wchar_t> , for example.

But why use this template? Just use std::fstream , aka std::basic_fstream<char> ; or std::wfstream , aka std::basic_fstream<wchar_t> .

It also wouldn't hurt to get rid of using namespace std; while your at it , too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM