简体   繁体   中英

Compiling error with std::min_element

I have been struggling with C2440 compiling error when I use std::min_element:

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:

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:

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. I was wondering what element in Geocoordinatefloat makes the compiling error. Sorry, I cannot give the definition of Geocoordinatefloat as it is a very big class. 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 . This means that begin() and end() return const_iterator ; and so does the specialisation of min_element that takes them as arguments.

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 .

You don't need a class, and you need to make the arguments bindable to 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>);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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