fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. namespace test {
  5. struct myClass {
  6. std::string _name;
  7.  
  8. explicit myClass(std::string name) : _name{ name } {}
  9. void print(std::ostream& os) const { os << _name; }
  10. };
  11.  
  12. std::ostream& operator<<(std::ostream& os, const myClass& mC) {
  13. mC.print(os); return os;
  14. }
  15.  
  16. namespace debug {
  17. std::ostream& operator<<(std::ostream& os, const myClass& mC) {
  18. os << "debug: ";
  19. mC.print(os);
  20. return os;
  21. }
  22. }
  23. }
  24.  
  25. test::myClass mc{"Kai"};
  26.  
  27. void run() {
  28. using namespace test;
  29. std::cout << mc << std::endl;
  30. }
  31.  
  32. void runDebug() {
  33. //using test::debug::operator<<;
  34. std::cout << mc << std::endl; // das erzeugt den Fehler
  35. // test::debug::operator<<(std::cout, mc) << std::endl; // so klappt das
  36.  
  37. }
  38.  
  39. int main() {
  40. run();
  41. runDebug();
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5376KB
stdin
Standard input is empty
stdout
Kai
Kai