简体   繁体   English

使用std :: min_element编译错误

[英]Compiling error with std::min_element

I have been struggling with C2440 compiling error when I use std::min_element: 使用std :: min_element时,我一直在努力解决C2440编译错误:

struct compare_x_coordinate 
{
  bool operator() (Geocoordinatefloat i,Geocoordinatefloat j) { return i.x_<j.x_; }
} mycompare;

vector<Geocoordinatefloat>::iterator it_min;
vector<Geocoordinatefloat> ptArray;
ptArray.push_back(...)
ptArray.push_back(...)
...

it_min = std::min_element(ptArray.begin(),ptArray.end(),mycompare); // Line 475 Compiling error C2440

std::min_element(ptArray.begin(),ptArray.end(),mycompare);           // Right

The compiling error with VC2010 is: VC2010的编译错误是:

Error   19  error C2440: '=' : cannot convert from 'const geometric::Geocoordinate<T> ' to 'geometric::Geocoordinate<T> *'  c:\research\projectivecorrection\code\iris\geometriccorrection\src\correspondingpoints\cp_hybrid.cpp    475

It is true that Geocoordinatefloat is a complicated class, and if I redefine class Geocoordinatefloat in a very simple way: 确实,Geocoordinatefloat是一个复杂的类,如果我以非常简单的方式重新定义Geocoordinatefloat类,则:

class Geocoordinatefloat
{
public:
  int x;
  int y;
  float Func()
  {
      return 1;
  };
  virtual void Fun2()
  {
      cout<<"hello"<<endl;
  };

};

It will work. 它会工作。 But for my program, it is impossible to change the definition of the class Geocoordinatefloat. 但是对于我的程序而言,无法更改Geocoordinatefloat类的定义。 I was wondering what element in Geocoordinatefloat makes the compiling error. 我想知道Geocoordinatefloat中的哪个元素会导致编译错误。 Sorry, I cannot give the definition of Geocoordinatefloat as it is a very big class. 抱歉,我无法给出Geocoordinatefloat的定义,因为它是一个非常大的类。 Thanks for any suggestion. 感谢您的任何建议。

EDIT: With the request, I make a simple program to repeat the error: 编辑:根据要求,我做了一个简单的程序来重复该错误:

#include <iostream>
#include <vld.h> 
#include <map>
#include <set>
#include <iostream>
#include <algorithm>
#include <vector>


using namespace std;



template <typename T>
class  Geocoordinate 
{
public:
    Geocoordinate() {};
    ~Geocoordinate() {};
     T x_;
     T y_;
 };

typedef Geocoordinate<float> Geocoordinatefloat;
typedef vector<Geocoordinatefloat> PointArray;


struct compare_x_coordinate 
{
    bool operator() (const Geocoordinatefloat &i,const Geocoordinatefloat &j) { return i.x_<j.x_; }
} mycompare;

void find_left_right_eignpoints(const PointArray &ptArray, 
        Geocoordinatefloat &left)
{
        vector<float> x_cord;
        PointArray::iterator it_min;
        std::min_element(ptArray.begin(),ptArray.end(),mycompare); 

        /******************************************************************************************
        // Error    1   error C2440: '=' : cannot convert from 'const Geocoordinate<T> 
        // ' to 'Geocoordinate<T> *'    c:\main.cpp 41
        */
        it_min = std::min_element(ptArray.begin(),ptArray.end(),mycompare);  // error code is here

        int index_min = it_min-ptArray.begin();
        left    = ptArray[index_min];

}



int  main(int argc, char* argv[])
{


    return 0;

}

The problem is that ptArray is const . 问题是ptArrayconst This means that begin() and end() return const_iterator ; 这意味着begin()end()返回const_iterator and so does the specialisation of min_element that takes them as arguments. 以它们为参数的min_element的专业化也是min_element

A const_iterator can't be assigned to a variable of type iterator , so you will need to change the type of it_min to PointArray::const_iterator . 不能将const_iterator分配给iterator类型的变量,因此您需要将it_min的类型it_minPointArray::const_iterator

You don't need a class, and you need to make the arguments bindable to const-references. 您不需要类,并且需要使参数可绑定到const-references。 Something like this: 像这样:

template <typename T>
bool compare_x(Geocoordinate<T> const & a, Geocoordinate<T> const & b)
{
    return a.x_ < b.x_;
}

Usage: 用法:

std::vector<Geocoordinatefloat>::iterator it_min =
    std::min_element(ptArray.begin(), ptArray.end(), compare_x<float>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM