Reputation: 23
as homework we need to build a generic map that will work with a given unmodifiable code:
class startsWith {
char val;
public:
startsWith(char v) : val(v) {};
bool operator()(const std::string& str) {
return str.length() && char(str[0]) == val;
}
};
void addThree(int& n) {
n += 3;
}
int main() {
Map<std::string, int> msi;
msi.insert("Alice", 5);
msi.insert("Bob", 8);
msi.insert("Charlie", 0);
// add To every name with B 3 points, using MapIf
startsWith startWithB('B');
MapIf(msi, startWithB, addThree);
}
I wrote:
template<typename T, typename S, typename Criteria, typename Action>
class MapIf {
public:
void operator() (Map<T,S>& map, Criteria criteria, Action act) {
for (typename Map<T, S>::iterator iter = map.begin(); iter != map.end(); ++iter) {
if (criteria(((*iter).retKey()))) {
act(((*iter).retData()));
}
}
}
};
and I get the error
Description Resource Path Location Type
missing template arguments before '(' token main.cpp /ex4 line 46 C/C++ Problem
in the given code (in MapIf(msi, startWithB, addThree);
)
How can I fix it? (I can only change my code)
Upvotes: 0
Views: 4887
Reputation: 14390
It looks like MapIf
should be a function, not a class:
template<typename T, typename S, typename Criteria, typename Action>
void MapIf(Map<T, S>& map, Criteria criteria, Action act)
{
for (typename Map<T, S>::iterator iter = map.begin(); iter != map.end(); ++iter) {
if (criteria(iter->retKey())) {
act(iter->retData());
}
}
};
Upvotes: 3