【发布时间】:2011-02-19 23:17:06
【问题描述】:
我正在创建一个 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 类的
有什么想法吗?提前致谢。
【问题讨论】:
标签: c++ templates overloading operator-keyword