We have a custom Logging class which compiles fine in VisualStudio 2010, but throws errors when being compiled with g++ on Linux. The error message we receive are the following:
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"
The respective lines of code are the following:
/*: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;
}
Other methods overloading the << operator work fine, therefore my guess is that the errors have something to with the argument type (std::_Smanip); any clues as to why?
Thanks,
Ben
_Smanipis a Microsoft extension, and not part of the standard library. That’s why your code compiles under Visual C++.Here’s an MSDN article on the use of
_Smanip, and here’s another one of how to avoid using it and write portable code instead.EDIT: Found another link that explains manipulators with parameters in great detail. They discuss methods to implement custom ones as well.