函数重载在模板化的max_element中失败

时间:2018-04-12 21:18:28

标签: c++ boost overloading

我很困惑,为什么我不能在std::max_element(absMcol.begin(), absMcol.end(), comp)的以下函数中为Minvb重载“comp”。

#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <boost/numeric/interval.hpp>
#include <boost/numeric/interval/io.hpp>

bool comp(double a, double b) {
        return (a < b);
    }

bool comp(boost::numeric::interval<double> a, boost::numeric::interval<double> b) {
    return (a.upper() < b.lower());
}

template<typename T>
void Minvb(T (&x)[2], T M[2][2], T b[2]) {
    T temp1[2];
    int maxrow;
    T l, rhs, temp2;
    for(int i = 0; i < 2; i ++) {
        x[i] = 0;
    }
    std::vector<T> absMcol;
    for(int i = 0; i < 2; ++ i) {
        for(int j = i; j < 2; ++ j) absMcol.push_back(abs(M[j][i]));
        maxrow = std::distance(absMcol.begin(), std::max_element(absMcol.begin(), absMcol.end(), comp) );
        maxrow = maxrow + i;
        absMcol.clear();
    }
}

int main() {
    boost::numeric::interval<double> x[2];
    boost::numeric::interval<double> p11(0.74889637381797358,0.7488963738179738);
    boost::numeric::interval<double> p12(0.1319999952,0.1319999952);
    boost::numeric::interval<double> p21(17.152090001094443,17.152090001094461);
    boost::numeric::interval<double> p22(2.7360000000000002,2.7360000000000002);  
    boost::numeric::interval<double> M[2][2] = {{p11,p12},
                                                {p21,p22}};
    boost::numeric::interval<double> b[2] = {p12,p22};

    double xx[2];
    double pp11=2.7360000000000002;
    double pp12=0.47220448613138766;  
    double pp21=2.011999999999999;  
    double pp22=14.234701687428823;  
    double MM[2][2] = {{pp11,pp12},                                                      
                       {pp21,pp22}};  
    double bb[2] = {pp12,pp22};  
    Minvb(x,M,b);  
    Minvb(xx,MM,bb);

    return 0;
}

我收到错误报告说:

no matching function for call to ‘max_element(std::vector< boost::numeric::interval< double>, std::allocator< boost::numeric::interval< double> > >::iterator,
std::vector< boost::numeric::interval< double>, std::allocator< boost::numeric::interval< double> > >::iterator, < unresolved overloaded function type>)’  

和另一个错误:

 error: no matching function for call to ‘max_element(std::vector< double, std::allocator< double> >::iterator, std::vector< double, std::allocator< double> >::iterator, < unresolved overloaded function type>)’

删除

bool comp(double a, double b) {
    return (a < b);
}

使代码有效,但这不是我想要的,因为我希望max_element()适用于double类型和boost::numeric::interval< double>类型。

注意到我不能为<类重载运算符interval,其中定义了boost::numeric::interval< double>类型数据,因为我不想触及{{1在boost库中定义。是否有人可以帮助我?先感谢您!

0 个答案:

没有答案