<分区>
使用带有 --std=c++14 的 gcc 版本 5.2.0 (GCC),如果命名空间 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;
}
我从运算符 << uncommented 得到的错误消息是:
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;
^