【发布时间】:2014-07-28 18:48:41
【问题描述】:
我想实现里面有map的容器,实现插入key/value和key取值的操作。 模板使用经验不多,找不到错误。
- 第 1 行:错误 C2663:std::_Tree<_traits>::find:错误 C2663::2 重载不合法;
- 第 2 行:错误 c2664:std::pair<_ty1> std::_Tree<_traits>::insert(std::pair &&): param 1 from "std::pair<_ty1>" в "std::pair<_ty1> &&";
所以我有接口:
IContainer.h
class ElemNotFound {};
template <class ElemType, class IndexType>
class IContainer
{
public:
virtual ~IContainer() {};
virtual const ElemType& GetElem( const IndexType& index ) const throw ( ElemNotFound ) = 0;
virtual void PutElem( const IndexType& index, const ElemType& elem ) throw () = 0;
};
还有
Container.h
#include <map>
#include "IContainer.h"
template <class ElemType, class IndexType>
class Container: public IContainer <ElemType, IndexType>
{
private:
typedef std::map<ElemType, IndexType> CMap;
CMap myMap;
public:
inline const ElemType& GetElem( const IndexType& index ) const throw ( ElemNotFound ) {
auto it = myMap.find(index); // line 1
if (toRet == end()) {
throw ElemNotFound();
}
return toRet->second;
}
inline void PutElem( const IndexType& index, const ElemType& elem ) throw () {
myMap.insert(make_pair(index, elem)); // line 2
}
};
int main()
{
Container < string, int> c;
c.PutElem(1, "as");
c.PutElem(2, "G");
c.GetElem(2);
return 0;
}
【问题讨论】:
-
map::find需要key_type(在您的情况下为ElemType)。你传递给它一个IndexType。你的意思是写typedef std::map<IndexType, ElemType> CMap;?