,-6ren">
gpt4 book ai didi

c++ "error: no matching function for call to"在集合中计数时

转载 作者:行者123 更新时间:2023-11-30 01:51:14 32 4
gpt4 key购买 nike

我正在使用我作为练习制作的集合和元组类实现 map ,但我不断收到此错误:error: no matching function for call to 'std::set<Tuple<int, char>, std::less<Tuple<int, char> >, std::allocator<Tuple<int, char> > >::count(Tuple<char, int>&)

这是我的代码:

        template <class K,class V> class Tuple
{
private:
K* mKey;
V* mValue;
public:
Tuple(const K& key, const V& val)
{
mKey = &key;
mValue = &val;
};

Tuple(const K& key)
{
mKey = &key;
};
const K& getKey()
{
return *mKey;
};
const V& getValue()
{
return *mValue;
};
void setKey(const K& key)
{
mKey=&key;
};
void setValue(const V& val)
{
mValue=&val;
};
bool operator< (Tuple<K,V>& t)
{
return *mKey<*t.mKey;
};
bool operator== (Tuple<K,V>& t)
{
return *mKey==*t.mKey;
};
};

template <class K,class V> class map
{
private:
set<Tuple<K,V> >* mTupleMap;

public:
map()
{
mTupleMap = new set<Tuple<K,V> >;
};

void insert(const K& key,const V& val)
{
Tuple<V,K> tmp(key,val);
if(mTupleMap->count(tmp)!=0)
{
typename set<Tuple<K,V> >::iterator it;
it=mTupleMap->find(tmp);
(*it).setValue(val);
}
else
{
mTupleMap->insert(tmp);
}
}
const V& getValue(const K& key)
{
set<V,K> tmp(key);
if(mTupleMap->count(tmp)!=0)
{
typename set<Tuple<K,V> >::iterator it;
it=mTupleMap->find(tmp);
return (*it).getValue();
}
return NULL;
}
};

int main()
{
map<int,char> mapIntToChar;
mapIntToChar.insert(1,'a');
mapIntToChar.insert(2,'b');
mapIntToChar.insert(1,'c');
cout << mapIntToChar.getValue(1) << endl;

return 0;
}

我不知道问题是什么或如何解决,所以我想获得一些帮助。

最佳答案

你的 tmpTuple<V, K> ,但是你的 set包含 Tuple<K, V>你有operator <仅适用于类型 Tuple<K,V> .

关于c++ "error: no matching function for call to"在集合中计数时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26360705/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com