Brady Dean Brady Dean - 1 year ago 184
C++ Question

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");

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

if(!f.is_open()) {
f.clear();, ios_base::out);

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();

Answer Source

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, a.k.a. std::basic_fstream<char>; or std::wfstream, a.k.a. std::basic_fstream<wchar_t>.

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