No match for 'operator<<' (ver. issue?)

C++ Newbie Issue

I’m a newbie with C++ and have been trying to set up NetBeans and run a C++ program on my MacBook Air via a virtual machine (UTM). The program runs successfully on my company’s Linux desktop, but the same source code does not work on my virtual Linux. The error message I get is error: no match for ‘operator<<’.

I am trying to understand the code of an employee who has left the company. I have seen several posts about this same issue, but none of the solutions I have found have worked in my situation.

Below is the error message and relevant source code. Could you help me look into them?

class logger
    {
      public:
        typedef std::ostringstream collector_stream_type;

      private:
        collector_stream_type _os;
        log_levels _messageLevel;

       public:
           logger(const logger & r) : _messageLevel(r._messageLevel)
              {
                   if ( do_log() ) _os << r._os; // this is where the error occurs.
               }
    }

Error / Warning message from NetBeans

In file included from

  • error: no match for ‘operator<<’ (operand types are ‘MySoftware::log::logger::collector_stream_type’ {aka ‘std::__cxx11::basic_ostringstream’} and ‘const collector_stream_type’ {aka ‘const std::__cxx11::basic_ostringstream’})
  • note: cannot convert ‘r.MySoftware::log::logger::_os’ (type ‘const collector_stream_type’ {aka ‘const std::__cxx11::basic_ostringstream’}) to type ‘const std::error_code&’
  • note: ‘const collector_stream_type’ {aka ‘const std::__cxx11::basic_ostringstream’} is not derived from ‘const std::complex<_Tp>’
  • note: cannot convert ‘((MySoftware::log::logger*)this)->MySoftware::log::logger::_os’ (type ‘MySoftware::log::logger::collector_stream_type’ {aka ‘std::__cxx11::basic_ostringstream’}) to type ‘std::byte’
  • note: candidate: ‘operator<<(int, int)’ (built-in)
  • note: ‘const collector_stream_type’ {aka ‘const std::__cxx11::basic_ostringstream’} is not derived from ‘const std::__shared_ptr<_Tp, _Lp>’
  • note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘std::__cxx11::basic_ostringstream’…

Issue

I’m a newbie with C++ and I’m trying to understand a program written by a former employee. The code runs successfully on my company’s Linux desktop, but not my virtual Linux. I get the error message error: no match for ‘operator<<’ when I try to compile the same source code.

I have seen several posts here about this same issue, but none of the solutions I have found have worked. Below is the source code and error message. Can you help me look into them?

Source code:

class logger
    {
      public:
        typedef std::ostringstream collector_stream_type;

      private:
        collector_stream_type _os;
        log_levels _messageLevel;

       public:
           logger(const logger & r) : _messageLevel(r._messageLevel)
              {
                   if ( do_log() ) _os << r._os; // this is where the error occurs.
               }
    }

Error / Warning message from NetBeans

In file included from

  • error: no match for ‘operator<<’ (operand types are ‘MySoftware::log::logger::collector_stream_type’ {aka ‘std::__cxx11::basic_ostringstream’} and ‘const collector_stream_type’ {aka ‘const std::__cxx11::basic_ostringstream’})
  • note: cannot convert ‘r.MySoftware::log::logger::_os’ (type ‘const collector_stream_type’ {aka ‘const std::__cxx11::basic_ostringstream’}) to type ‘const std::error_code&’
  • note: ‘const collector_stream_type’ {aka ‘const std::__cxx11::basic_ostringstream’} is not derived from ‘const std::complex<_Tp>’
  • note: cannot convert ‘((MySoftware::log::logger*)this)->MySoftware::log::logger::_os’ (type ‘MySoftware::log::logger::collector_stream_type’ {aka ‘std::__cxx11::basic_ostringstream’}) to type ‘std::byte’
  • note: candidate: ‘operator<<(int, int)’ (built-in)
  • note: ‘const collector_stream_type’ {aka ‘const std::__cxx11::basic_ostringstream’} is not derived from ‘const std::__shared_ptr<_Tp, _Lp>’
  • note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘std::__cxx11::basic_ostringstream’…

The error message error: no match for ‘operator<<’ suggests that there is no defined operator<< for the collector_stream_type class in your code.

To resolve this issue, you need to define the operator<< for the collector_stream_type class. Here’s an example of how you can do it:

class logger
{
public:
    typedef std::ostringstream collector_stream_type;

private:
    collector_stream_type _os;
    log_levels _messageLevel;

public:
    logger(const logger &r) : _messageLevel(r._messageLevel)
    {
        if (do_log())
            _os << r._os;
    }

    // Define the operator<< for the collector_stream_type class
    friend collector_stream_type &operator<<(collector_stream_type &os, const collector_stream_type &other)
    {
        os << other.str(); // Use the str() function to get the string representation of the other stream
        return os;
    }
};

With this definition, the operator<< will be able to handle the collector_stream_type class and resolve the error.