簡體   English   中英

未定義模板的隱式實例化:basic_fstream<string>

[英]implicit instantiation of undefined template: basic_fstream<string>

我正在使用 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>

擺脫using namespace std; 而你也這樣做了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM