使用递归方法的类,使用int矢量元素作为输入



我正在尝试制作一个具有递归二进制运算符的类,但由于某种原因,我一直存在这些错误:

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

to

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

正如@o_weisman所提到的。

两行:

return ReduceGeneric(v,i);

to

return ReduceGeneric::reduce(v,i);

您需要调用功能而不是类。

最新更新