g ++"声明"运算符<<"作为非函数"

Ben*_*Ben 4 c++ g++ operator-overloading

我们有一个自定义的Logging类,可以在VisualStudio 2010中编译好,但在Linux上使用g ++编译时会抛出错误.我们收到的错误消息如下:

Logger.hpp:84: error: declaration of "operator<<" as non-function
Logger.hpp:84: error: expected ";" before "(" token
Logger.hpp:91: error: expected ";" before "inline"
Logger.hpp:91: error: declaration of "operator<<" as non-function
Logger.hpp:91: error: expected ";" before "(" token
Logger.hpp:98: error: expected ";" before "typedef"
Run Code Online (Sandbox Code Playgroud)

各行代码如下:

/*:84*/inline Logger& operator<<(std::_Smanip<std::ios_base::fmtflags> output)
{
        if (this->loggingEnabled())
                std::cout << output;
        return *this;
}

inline Logger& operator<<(std::_Smanip<std::streamsize> output)
{
        if (this->loggingEnabled())
                std::cout << output;
        return *this;
}

typedef std::basic_ostream<char, std::char_traits<char> >& (*StdEndl)(std::basic_ostream<char, std::char_traits<char> >&);
inline Logger& operator<<(StdEndl output)
{
        if (this->loggingEnabled())
                std::cout << output;
        return *this;
}
Run Code Online (Sandbox Code Playgroud)

重载<<运算符的其他方法工作正常,因此我的猜测是错误与参数type(std::_Smanip)有关; 关于为什么的任何线索?

谢谢,本

Pra*_*ian 5

_Smanip是Microsoft扩展,不是标准库的一部分.这就是你的代码在Visual C++下编译的原因.

这是一篇关于使用的MSDN文章_Smanip,这是另一个如何避免使用它并编写可移植代码的文章.

编辑:找到另一个链接,详细解释带有参数的操纵器.他们还讨论了实现自定义方法的方法.