模板类中的关系运算符重载 (C++)
我正在创建一个 KeyValuePair 类,并且在重载关系运算符时遇到一些麻烦。据我了解,这是使用 std 排序函数所必需的(我试图根据值进行排序)
这是标题:
template <typename K, typename V>
class KeyValuePair
{
public:
//factory
static KeyValuePair<K,V>* newKeyValuePair(K key, V value);
//getters
const K &Key() const;
const V &Value() const;
//setter
V &Value();
//The problem
bool operator<(const KeyValuePair<K,V> &rhs);
string toString();
~KeyValuePair(void);
private:
K key;
V value;
KeyValuePair(K key, V value);
KeyValuePair(void);
};
这是 << 的定义。 。
template <typename K, typename V>
bool KeyValuePair<K,V>::operator<(const KeyValuePair<K,V> &rhs)
{
return value < rhs.Value();
}
这是我正在测试该类功能的主要部分
int _tmain(int argc, _TCHAR* argv[])
{
KeyValuePair<char,int>* kvp1 = KeyValuePair<char, int>::newKeyValuePair('A',1);
KeyValuePair<char,int>* kvp2 = KeyValuePair<char,int>::newKeyValuePair('B',10);
cout << (kvp1 < kvp2) << "\n";
return 0;
}
我在 < 处有一个断点我的 KeyValuePair 类中的函数,并且它从未被激活。
有什么想法吗?提前致谢。
I am creating a KeyValuePair class, and am running into some trouble when overloading the relational operators. It is my understanding that this is necessary for using the std sort functions (I am trying to have sort based on the values)
Here is the header:
template <typename K, typename V>
class KeyValuePair
{
public:
//factory
static KeyValuePair<K,V>* newKeyValuePair(K key, V value);
//getters
const K &Key() const;
const V &Value() const;
//setter
V &Value();
//The problem
bool operator<(const KeyValuePair<K,V> &rhs);
string toString();
~KeyValuePair(void);
private:
K key;
V value;
KeyValuePair(K key, V value);
KeyValuePair(void);
};
Here is the definition of the < function
template <typename K, typename V>
bool KeyValuePair<K,V>::operator<(const KeyValuePair<K,V> &rhs)
{
return value < rhs.Value();
}
And here is the main where I am just testing the functionality of the class.
int _tmain(int argc, _TCHAR* argv[])
{
KeyValuePair<char,int>* kvp1 = KeyValuePair<char, int>::newKeyValuePair('A',1);
KeyValuePair<char,int>* kvp2 = KeyValuePair<char,int>::newKeyValuePair('B',10);
cout << (kvp1 < kvp2) << "\n";
return 0;
}
I have a breakpoint at the < function in my KeyValuePair class, and it is never activated.
Any Ideas? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
kvp1
和kvp2
是指向KeyValuePair
对象的指针。它们本身不是KeyValuePair
对象。<代码>*kvp1 < *kvp2 将调用重载的
运算符<
。您不能重载两种指针类型的operator<
,因为将使用指针的内置operator<
。std::pair
可以用作键值对。无论如何,您几乎肯定不应该动态创建这种类型的对象:您应该尽可能避免动态分配,尤其是显式动态分配。相反,只需使用KeyValuePair
局部变量:kvp1
andkvp2
are pointers toKeyValuePair<char, int>
objects. They are not themselvesKeyValuePair<char, int>
objects.*kvp1 < *kvp2
would invoke your overloadedoperator<
. You cannot overloadoperator<
for two pointer types because the built-inoperator<
for pointers will be used.std::pair
can be used as a key-value pair. In any case, you almost certainly shouldn't be dynamically creating objects of this type: you should prefer to avoid dynamic allocation wherever possible, especially explicit dynamic allocation. Instead, just useKeyValuePair<char, int>
local variables: