c++ - 使用 int vector 元素作为输入的递归方法类

标签 c++ class recursion vector

我正在尝试创建一个类,该类具有从 vector 获取输入的递归二元运算符,但由于某种原因,我一直遇到这些错误:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
In member function 'int ReduceGeneric::reduce(std::vector<int>&, std::vector<int>::iterator)':|
|14|error: no matching function for call to 'ReduceGeneric::binaryOperator(std::vector<int>::iterator, int&)'|
|14|note: candidate is:|
|8|note: virtual int ReduceGeneric::binaryOperator(int, int)|
|8|note:   no known conversion for argument 1 from 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' to 'int'|
|16|error: invalid cast to abstract class type 'ReduceGeneric'|
|4|note:   because the following virtual functions are pure within 'ReduceGeneric':|
|8|note:     virtual int ReduceGeneric::binaryOperator(int, int)|
|22|error: invalid cast to abstract class type 'ReduceGeneric'|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

这是代码:

#include <iostream>
#include <vector>

class ReduceGeneric {
public:
    int reduce(std::vector<int>& v, std::vector<int>::iterator i); //set i=v.begin() in main
private:
    virtual int binaryOperator(int m, int n) =0;
    int result;
};

int ReduceGeneric::reduce(std::vector<int>& v, std::vector<int>::iterator i)   {
    if (i==v.begin())   {
        result=binaryOperator(v.begin(),*i);
        i++;
        return ReduceGeneric(v,i);
    } else if (i==v.end()) {
        return result;
    }   else    {
        result=binaryOperator(result,*i);
        i++;
        return ReduceGeneric(v,i);
    }
}

class ReduceMinimum : public ReduceGeneric  {
private:
    int binaryOperator(int m, int n);
};

int ReduceMinimum::binaryOperator(int i, int j) {
    if (i<=j)   {
        return i;
    }   else    {
        return j;
    }
}

非常感谢。我是编程新手,所以任何输入都是很好的输入。显然,我的帖子充满了代码,因此我正在尝试添加更多文本。我喜欢披萨。

最佳答案

您需要更改这些行才能成功构建:

result=binaryOperator(v.begin(),*i);

result=binaryOperator(*v.begin(),*i);

正如@o_weisman 所提到的。

两行:

return ReduceGeneric(v,i);

return ReduceGeneric::reduce(v,i);

因为你需要调用函数而不是类。

关于c++ - 使用 int vector 元素作为输入的递归方法类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46137965/

相关文章:

javascript - Node javascript递归内存不足

c++ - 需要帮助创建循环

c++ - OpenCV 错误:断言在 cv::Mat 行 522 中失败

C++ 何时在模板定义中衰减

javascript - 按类添加事件监听器,在 JS 中将 ID 作为参数传递

typescript - 如何在 TypeScript 中链接/连接/关联两个类字段的类型?

html - 一个CSS类可以包含另外两个类吗

javascript - 在javascript中生成深层对象中所有属性链的数组

c++ - 给char/unsigned char赋值

python - 计算变化的 Baktracking 函数超过最大递归深度