C ++ compilation error while debugging, but not in release

I am trying to compile a small dll COM library (using visual studio 2008 pro) and it compiles fine in the version, but when I try to compile it in debug, I get a compilation error:

error C2664: 'bool (MyClass &, double)': cannot convert parameter 2 from "MyClass" to "double".

Now this error comes from a line in the code where I do this (note that someValueThatIsADouble is of type double):

std::vector<MyClass>::iterator iter = std::lower_bound(MyVector.begin(), MyVector.end(), someValueThatIsADouble, less); 

And the smaller the function is defined this way:

 bool less(MyClass& a, double b); 

I don’t understand why I get this error, and if there is a good reason for this error, why do I only get it in Debug (and not in Release)? The DLL, when compiled in Release, works fine and does not crash. Also, I checked and #ifdef DEBUG or such things that could change the code that was compiled in debug and release do not exist.

EDIT:

I myself did not write the code, and this is an algorithm that I know little about, so I don’t know what should represent a double value, and I do not want to change the logic inside less to use MyClass instead of double as the second parameter.

 class MyClass { public : MyClass(): _dValue1(0.0),_dValue2(0.0),_dValue3(0.0) { } MyClass(double dValue1, double dValue3, double dValue2): _dValue2(dValue2),_dValue3(dValue3),_dValue1(dValue1) { } ~MyClass() {} double getValue1() {return _dValue1;} double getValue3() {return _dValue3;} double getValue2() {return _dValue2;} double _dValue1; double _dValue3; double _dValue2; public: friend class vector<MyClass>; int compare(const MyClass & t1, const MyClass & t2) { if (t1._dValue1 < t2._dValue1) return -1; else if (t2._dValue1 < t1._dValue1) return 1; else return 0; } bool operator> (const MyClass & rhs) { if ( _dValue1 > rhs._dValue1) return true; else return false; } bool operator< (const MyClass & rhs) { if ( _dValue1 < rhs._dValue1) return true; else return false; } }; 

Edit:

MSalters answer showed that the implementations of the predicates for debugging and release were not the same, which made it compile in the release, and not in debugging in my case (because the code is not very neat and should not use a comparison function with 2 different types). The hack I did to use this code in debugging was also to put this line before any inclusions (note that the preferred solution should be to have a better comparison function, but in my case it was not possible ):

 #define _HAS_ITERATOR_DEBUGGING 0 
+4
source share
3 answers

The error message suggests that you are using MSVC. This library implementation contains debug predicate checks. In particular, for a partial order predicate (like your less ), I think it checks if Pred(a,b) && Pred(b,a) == false . Obviously, it will not work here.

(One of the common predicate errors is that people used to determine order such as a<b and b<a . In this case, a lot of <algorithm> breaks, so there are some debug checks to prevent such errors. However, they only catch errors, if you really manage to pass a couple of bad values a,b at runtime, they cannot catch theoretical errors at compile time)

+2
source

Try:

 bool less(const MyClass& a, const MyClass& b); 
+1
source
 template<class ForwardIterator, class Type, class BinaryPredicate> ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const Type& val, BinaryPredicate comp); 

first: Forward iterator that addresses the position of the first element in the search range.

last: A promising iterator that addresses the position located behind the end element in the search range.

val: the value in which the first position or possible first position in the given range is searched.

comp: A custom predicate function object that defines the meaning in which one element is smaller than the other. The binary predicate takes two arguments and returns true when executed, and false when not executed.

It compiles in the release because some checks are deactivated in the release std version. But it should not compile: val must be of type MyClass, not of type double, and then it will not compile less even in the release.

+1
source

All Articles