【发布时间】:2018-08-09 12:49:19
【问题描述】:
使用 gcc 版本 5.2.0 (GCC) 和 --std=c++14,如果取消注释命名空间 MyNamespace 中注释掉的运算符 ostream,则以下代码不再编译。这是错误还是功能? (用g++ -c --std=c++14 x.cxx编译)
#include <string>
#include <iostream>
typedef std::pair<std::string, std::string> StringPair;
std::ostream& operator<<( std::ostream&, const StringPair &pair) {
std::cout <<pair.first<<"."<<pair.second;
}
namespace MyNamespace {
class MyClass {};
//std::ostream& operator<< (std::ostream&, const MyClass &);
void xxx ();
}
void MyNamespace::xxx () {
StringPair pair;pair.first="1";pair.second="2";
std::cout <<pair<<std::endl;
}
我使用运算符
x.cxx: In function ‘void MyNamespace::xxx()’:
x.cxx:18:13: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘StringPair {aka std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >}’)
std::cout <<pair<<std::endl;
^
【问题讨论】:
-
首先您可能应该删除代码中的未定义行为,您需要从全局
operator<<函数中返回一些内容以获取该对。您可能应该使用作为参数传递的流而不是硬编码std::cout。不能解决你的问题,但不相关的问题往往会隐藏实际问题。 -
@Someprogrammerdude 我刚刚尝试并重现了报告的错误。我认为未定义的行为与它没有任何关系 - 这发生在编译期间
-
这与运行代码无关。这是关于编译它。当然这种行为没有意义,但这就是我得到的:-)
-
链接到 ideone 复制行为:ideone.com/Lgg5oL
-
@pbhd 我通过将
using ::operator<<添加到命名空间来编译它。但我不确定为什么需要它。您可以查看ideone.com/01ypI9
标签: c++ g++ operator-overloading language-lawyer ostream