C2065:'oss'未声明的标识符

C2065: 'oss' undeclared identifier

本文关键字:标识符 未声明 oss C2065      更新时间:2023-10-16

我使用Qt Creator,我在我的c++代码中有一个奇怪的错误。这段代码

#include <stdlib.h>
std::wstring readFile(const std::wstring& f) {
    //try and open the file
    std::wifstream file(TL::wToString(f).c_str());
    if (!file)
        throw BadFileLoad(f, L"Impossible to write in the file "+f);
    //reads the content
    std::wostringstream oss;
    oss << file.rdbuf();
    return oss.str();
}

给出这个错误:

C2065: 'oss'未声明的标识符

但实际上我在第一行就声明了!!怎么了?谢谢!

您需要包含具有std::wostringstream类的标题

#include <sstream>

您缺少一些包含文件。试试这样做:

#include <string>  // std::wstring
#include <sstream> // std::wostringstream
#include <fstream> // std::wifstream

我发现了错误,我注释了这行抛出BadFileLoad(f, L"Impossible to write in the file "+f);而不是if (!file)…对不起,伙计们,这是个愚蠢的问题!感谢您的宝贵时间!