获取错误:在"("令牌之前缺少模板参数

Getting the error:missing template arguments before '(' token

本文关键字:参数 令牌 取错误 获取      更新时间:2023-10-16

作为家庭作业,我们需要构建一个通用映射,该映射将适用于给定的不可修改的代码:

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);
}

我写道:

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()));
            }
        }
    }
};

我收到错误

Description Resource    Path    Location    Type
missing template arguments before '(' token main.cpp    ‪/ex4‬  line 46 C/C++ Problem

在给定的代码中(以MapIf(msi, startWithB, addThree);为单位)

我该如何解决它?(我只能更改我的代码)

看起来MapIf应该是一个函数,而不是一个类:

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());
        }
    }
};