代码之家  ›  专栏  ›  技术社区  ›  PentiumPro200

C++向量迭代器N\u元素编译错误

  •  0
  • PentiumPro200  · 技术社区  · 8 年前

    下面的代码将无法编译。从第2行到最后一行(第n\u元素…)有错误。这似乎与比较器有关。编译器声称“该项的计算结果不是一个包含2个参数的函数”。如何修复编译错误?

        struct Result {
            Result(unsigned int id, double result);
            bool cmp(const Result &a, const Result &b)  const;
    
            unsigned int id;
            double result;
        };
    
    
    Result::Result(unsigned int id, double result) {
        this->id = id;
        this->result = result;
    }
    
    bool Result::cmp(const Result &a, const Result &b)  const {
        if(a.result < b.result) {
            return true;
        }
        return false;
    }
    
        //25th-percentile
        int index = (int) ((buffer.size()+1.0)/4.0 - 0.499);
        vector<Result>::iterator itrindex = buffer.begin() + index;
        nth_element(buffer.begin(), itrindex, buffer.end(), &Result::cmp);
        double twentyfifthperc = buffer[index].result;
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Jarod42    8 年前
    bool cmp(const Result &a, const Result &b)  const;
    

    应该是

    static bool cmp(const Result &a, const Result &b);