0
我想避免在插入map
時使用std::pair()
構造函數或std::make_pair()
函數。我也想知道插入操作的成功狀態,所以我不能使用operator[]
。我嘗試了下面的代碼,但它會產生一個編譯錯誤。模板類中聲明std :: pair的C++錯誤
template<typename TKey, typename TVal>
class Map
{
private :
std::map<TKey, TVal> m_holder;
public :
bool insert(TKey key, TVal val)
{
std::pair<std::map<TKey, TVal>::iterator, bool> ret;
/* ret = m_holder.insert(std::make_pair(key, val)); */
return 0;
}
};
int main()
{
return 0;
}
錯誤:
Hello.cpp: In member function `bool Map<TKey, TVal>::insert(TKey, TVal)':
Hello.cpp:13: error: type/value mismatch at argument 1 in template parameter list for `template<class _T1, class _T2> struct std::pair'
Hello.cpp:13: error: expected a type, got ` std::map<TKey,TVal,std::less<_Key>,std::allocator<std::pair<const _Key, _Tp> > >::iterator'
Hello.cpp:13: error: invalid type in declaration before ';' token
幫我解決這個問題。的
可能重複[在哪裏,爲什麼我必須把「模板」和「類型名稱」關鍵字?](http://stackoverflow.com/questions/610245/where-and-why-do-我必須把模板和類型名關鍵字) – 2014-10-01 13:20:25