c++ - 收到错误 :missing template arguments before '(' token

标签 c++

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

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

关于c++ - 收到错误 :missing template arguments before '(' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21138510/

相关文章:

c++ - 如何在带有初始值设定项的构造函数中使用 vprintf/cstdarg 功能?

c++ - 反向功能超出了我的cpp程序的范围

c++ - 两个指针什么时候可以比较?

c++ - 级联文件加载错误

php - 使用 PHP 编写脚本的游戏引擎?

c++ - 将位图位从 GetDIBits 转换为 jpeg 以进行 winsock 传输

c++ - 我如何知道 C++ 中异常的名称?

c++ - Linux 是否记录守护进程抛出的未捕获异常?

c++ - For 循环中的 While 循环

c++ - 以优雅的方式管理指向游戏引擎中被破坏对象的指针