1

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;

}
0

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

Thanks. But this also causes compiling problem for me. The main problem is if I assign a variable to std::min_element(ptArray.begin(),ptArray.end(),mycompare), it always fails.
@feelfree: Did you forget to dereference the iterator?
@feelfree: Oh, OK, indeed - the result type has to be a const_iterator if you only have a constant reference or pointer. Then won't be able to assign to the resulting iterator, of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.