c++operator-overloadingoperator-keywordoperands

no match for ‘operator<<’ in C++


I'm new to C++ and I am trying to reproduce the following code from pybeesgrid repo (https://github.com/berleon/pybeesgrid/blob/master/src/beesgrid.cpp)

namespace beesgrid {

std::string getLabelsAsString(const Grid::idarray_t & id_arr) {
    std::stringstream ss;
    for (size_t i = 0; i < id_arr.size(); i++) {
        const auto & id = id_arr.at(i);
        if (i % 4 == 0 && i != 0) {
            ss << ".";
        }
        ss << id;
    }
    return ss.str();
}
}

I am getting the error:

 error: no match for ‘operator<<’ (operand types are ‘std::stringstream’ {aka ‘std::__cxx11::basic_stringstream<char>’} and ‘const boost::logic::tribool’)
   17 |         ss << id;
      |         ~~ ^~ ~~
      |         |     |
      |         |     const boost::logic::tribool
      |         std::stringstream {aka std::__cxx11::basic_stringstream<char>}

From what I understood I need to overload the << operator to read the specific types I have. Is that right? Does anyone knows how to fix it?

Thanks.

I tried to add the line

std::stringstream& operator <<( std::stringstream &os,const id& id );

such as the code is now

namespace beesgrid {

std::string getLabelsAsString(const Grid::idarray_t & id_arr) {
    std::stringstream ss;
    for (size_t i = 0; i < id_arr.size(); i++) {
        const auto & id = id_arr.at(i);
        std::stringstream& operator <<( std::stringstream &os,const id& id );
        if (i % 4 == 0 && i != 0) {
            ss << ".";
        }
        ss << id;
    }
    return ss.str();
}
}

But this way also doesn't work and I get the error: variable "id" is not a type name


Solution

  • The problem is that you haven't overloaded operator<< for boost::logic::tribool.

    To solve this replace std::stringstream& operator <<( std::stringstream &os,const id& id ); with

    //----------------------------------------------------------vvvvvvvvvvvvvvvvvvvvv--->replaced id with boost::logic::tribool here in the second parameter
    std::stringstream& operator <<( std::stringstream &os,const boost::logic::tribool& id  );