Namespaces and operator resolution

前端 未结 4 1047
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 01:42

I am using a library that defines output stream operators (operator<<) in the global namespace. In my own namespace, I was always declaring such operators in the globa

4条回答
  •  猫巷女王i
    2020-12-01 02:36

    My answer is very similar to the others but in particular, the compiler is trying to find A::operator<<() because it's operating on something in the A namespace. If you want to invoke the one outside the namespace, you can call it explicitly by using

    ::operator<<(std::cout, A::MyClass();
    

    For smoother syntactic use, put it in the namespace.

提交回复
热议问题