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