如何解决为什么没有找到 operator<< 重载?
我已经为全局命名空间中的 operator<<
声明了一个 time_point
重载,并在 a::Foo
命名空间中为 a
声明了一个重载。当我尝试在命名空间 time_point
中也定义的函数中打印 a
时,出现错误:
二进制表达式的无效操作数。
这是为什么,最好的解决方案是什么?
#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();
}
解决方法
原因很简单:::operator<<
阴影您的{
"name":"Example","description":"description","version":"0.1","manifest_version":3,"background":{
"service_worker":"background.js"
},"permissions":[
"declarativeContent"
],"action":{
"default_icon":{
"16":"/images/get_started16.png","32":"/images/get_started32.png","48":"/images/get_started48.png","128":"/images/get_started128.png"
},"default_title":"press here to open"
},"icons":{
"16":"/images/get_started16.png","128":"/images/get_started128.png"
}
}
,因为它们具有相同的名称。这就是操作符基本上必须与其类共享命名空间的原因:这种阴影是意料之中的,因此 ADL(如评论中所述)是找到它们的唯一可靠方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。