C++ 通过wofstream打印到文件

C++ 通过wofstream打印到文件,c++,file,printing,wofstream,C++,File,Printing,Wofstream,以下代码不起作用,并给出编译错误: 严重性代码说明项目文件行抑制状态错误 二进制表达式('std::wofstream'(也称为'basic_of stream>')的操作数无效 代码是: template <class T> void printtofile(std::string filename, int mode, T var, std::wstring msg) { wofstream outfile; if (mode == 0) outfile.open

以下代码不起作用,并给出编译错误:

严重性代码说明项目文件行抑制状态错误 二进制表达式('std::wofstream'(也称为'basic_of stream>')的操作数无效

代码是:

template <class T>
void printtofile(std::string filename, int mode, T var, std::wstring msg)
{
    wofstream outfile;
    if (mode == 0) outfile.open(filename); else outfile.open(filename, ios::out | ios::app);
    outfile << msg << L"\n";
    outfile << var << L"\n";
    outfile.close();
}
模板
void printtofile(std::string文件名、int模式、T变量、std::wstring msg)
{
wofstream输出文件;
if(mode==0)outfile.open(filename);else outfile.open(filename,ios::out | ios::app);

outfile如果
outfile您需要重载
@Ted lynmo,在编译第一行代码时会出现错误:outfile我做了一个答案,让它更清楚。非常感谢您的回答。复制对我来说是另一个问题。我正在处理它。关于行:outfile,欢迎您!复制通过添加一个
main()
您使用导致编译错误的变量调用函数。我怀疑您没有在问题中披露完整的错误消息。它应该会显示类型,所以请将其完整复制/粘贴到问题中。如果另一个函数成功,则在调用该函数时您没有使用相同的类型。您是对的。我我没有粘贴整个错误消息,因为我认为它与此无关。但在复制代码后,我了解到它与此有关。我使用犰狳进行矩阵运算,这造成了问题。但是,如果我使用ofstream而不是wofstream,则不会产生错误。@KC_u我认为您的代码中可能混合了不相关的类型“不容易转换-但我不确定,因为你还没有说清楚。制作一个小程序,提出这个问题,然后提出另一个问题,人们通常会跳出来。是的,你说得对;我同时使用了wstring和string类型。我意识到wofstream不会打印特殊的unicode字符。现在我转换了。”将wstring转换为utf8字符串,并改用流。
    outfile << var << L"\n";
template <class T>
void printtofile(std::string filename, int mode, T var)
{
    wofstream outfile;
    if (mode == 0) outfile.open(filename); else outfile.open(filename, ios::out | ios::app);
    outfile << var << L"\n";
    outfile.close();
}
std::wofstream& operator<<(std::wofstream&, const T&);
template<class T>
auto printtofile(const std::string& filename, int mode, T var,
                 const std::wstring& msg)
    -> decltype(std::wofstream{} << var, void()) {
//...
}
error: no matching function for call to
 ‘printtofile(..., int, <the offending type>, std::wstring&)’