【发布时间】:2021-12-17 22:54:55
【问题描述】:
我有来自第三方的KeyA 类,将成为使用的关键。当我定义一个“
error: no match for ‘operator<’ (operand types are ‘const KeyA’ and ‘const KeyA’)
一些显示问题的简化代码:
#include <map>
using namespace std;
struct KeyA { // defined in somewhere else
int a;
};
namespace NS {
struct config {
using Key = KeyA; // type alias
using Table = map<Key, int>;
};
bool operator <(const config::Key& lhs, const config::Key& rhs) {
return lhs.a <rhs.a ;
}
}
int main()
{
using namespace NS;
config::Table table;
table[{1}]= 2;
return 0;
}
这里发生了什么?以及如何解决这个问题(无法触及KeyA,很可能必须将重载函数保留在NS)?
【问题讨论】:
-
NS 是我必须忍受的约束。 :-(
-
在你的键结构中将操作符设置为好友
-
为什么不能在与
KeyA相同的命名空间中定义operator<?您不必为此修改KeyA -
对不起,“KeyA”是一些不可触及的生成代码;而且我只能在“NS”中更改
标签: c++ namespaces operator-overloading type-alias