C++ Visual Studio 15:重载“<<&引用;C+中的插入运算符+;

C++ Visual Studio 15:重载“<<&引用;C+中的插入运算符+;,c++,visual-studio-2015,operator-overloading,C++,Visual Studio 2015,Operator Overloading,下面的代码在GNU GCC v7.1.1上运行良好,但在VS15上出现错误 #include <iostream> class Logger { public: //var should be initialized in init loge level function bool debug_log_level; friend Logger& operator << (Logger& p, const char* str);

下面的代码在GNU GCC v7.1.1上运行良好,但在VS15上出现错误

#include <iostream>

class Logger {
public:
    //var should be initialized in  init loge level function
    bool debug_log_level;

    friend Logger& operator << (Logger& p, const char* str);
    Logger& logs(std::string severity, bool level = 0);
    Logger& getInstance();
};

Logger logger;
Logger& instance= logger;

Logger& Logger::getInstance() {
    static Logger theInstance;
    return theInstance;
}

Logger& Logger::logs(std::string severity, bool level)
{
    //Unused(level) as init is one time activity and will use it's own variable.
    std::cout<<severity << ": ";
    return instance;
}

//will be invoked when RHS is class reference and LHS is "str"
Logger& operator<<(Logger& p, const char* str)
{
    if(p.debug_log_level)
        std::cout << str << std::endl;
    return instance;
}

int main()
{
    logger.debug_log_level=true;
   logger.logs("Info",0)<<"Ravi..";
   return 0;
}
#包括
类记录器{
公众:
//应在init loge level函数中初始化var
bool调试日志级别;

friend Logger&operator根据标准,我不确定哪种实现是正确的,但在这种情况下,似乎使用GCC
std::ostream&operatorTry add
#include
。请显示一个,例如
#include int main(){std::cout@songyuanyao:它正在工作。谢谢!你能解释更多关于它的信息作为回答吗?我已经为MCVE准备好了,但它确实失败了compile@RaviShinde您只需要包含所有必需的头文件。当
#include
时,GCC可能会隐式地包含
,但您不能依赖它。
#include <string>