错误来自type& AssocArray<key, value>::operator[]( key& k )
位于 AssocArray 之外。我很困惑为什么会收到错误消息:
error C2143: syntax error : missing ';' before '&'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
编辑:
我尝试了以下这些陈述:
typedef typename std::pair<key, value> type;
typedef std::pair<key, value> type;
using type = std::pair<key, value>;
他们也没有工作。
template <typename key, typename value>
class AssocArray
{
public:
typedef typename std::pair<typename key, typename value> type;
bool addItem(key& k, value& v);
bool isItem(key& k);
type& operator[] (key& k);
protected:
private:
std::vector<type> vecData;
};
template <typename key, typename value>
type& AssocArray<key, value>::operator[]( key& k )
{
auto iter = std::find_if(vecData.begin(), vecData.end(),
[&](type& param)
{
return param.first == key;
}
);
if(vecData.end() != iter)
return *iter;
value v;
vecData.push_back(std::make_pair(k, v));
return *vecData.rbegin();
}
template <typename key, typename value>
bool AssocArray<key, value>::isItem( key& k )
{
auto iter = std::find_if(vecData.begin(), vecData.end(),
[&](type& param)
{
return param.first == key;
}
);
return vecData.end() != iter;
}
template <typename key, typename value>
bool AssocArray<key, value>::addItem( key& k, value& v )
{
if(isItem(k)) return false;
vecData.push_back(std::make_pair(k, v));
return true;
}