I'm reading a book. Obviously there are some failures in it.
In the code below, how can I access plant::operator<< in run() and plant::debug::operator<< in diagnostic()? The compiler can't decide between multiple operator<< in method diagnostic(). How can I tell it which one to use?
#include <iostream>
#include <string>
namespace plant {
class Tree
{
std::string name_;
public:
explicit Tree(const std::string_view name) : name_{name} {}
void print(std::ostream& os) const { os << name_; }
};
std::ostream& operator<<(std::ostream& os, const Tree& arg)
{
arg.print(os); return os;
}
namespace examples {
}
namespace debug {
std::ostream& operator<<(std::ostream& os, const Tree& arg)
{
os << "DEBUG: .."; arg.print(os); return os;
}
}
}
plant::Tree baum{"Mein Baum"};
void run()
{
using namespace plant;
std::cout << baum << "\n";
}
void diagnostic()
{
using namespace plant::debug;
std::cout << baum << "\n";
}
int main() {
run();
diagnostic();
}
debug::operator<<(std::cout, baum);?operator<<in different namespaces is probably not the correct start.