samedi 5 juin 2021

Why is operator<< overload not found (C++)

I've declared an operator<< overload for time_point in the global namespace and also one for a::Foo in the a namespace. When I try to print a time_point in a function also defined in namespace a, I get ther error: Invalid operands to binary expression. Why is this, and what is the best solution?

#include <iostream>
#include <chrono>

std::ostream &operator<<(std::ostream &os,
                         const std::chrono::system_clock::time_point &time_point);
namespace a {
class Foo;
std::ostream &operator<<(std::ostream &os, const a::Foo &foo);
void print_time() {
  std::cout << std::chrono::system_clock::now(); // error here!
}
}

int main() {
  a::print_time();
}

Aucun commentaire:

Enregistrer un commentaire