简体   繁体   English

c++ 在调试中编译错误但在发布中没有

[英]c++ compile error in debug but not in release

I'm trying to compile a small COM dll (using visual studio 2008 pro), and it compiles fine in release, but when I try to compile it in debug I get a compilation error:我正在尝试编译一个小的 COM dll (使用visual studio 2008 pro),它在发布时编译得很好,但是当我尝试在调试中编译它时出现编译错误:

error C2664: 'bool (MyClass &, double)': cannot convert parameter 2 from 'MyClass' to 'double'.错误 C2664:“bool (MyClass &, double)”:无法将参数 2 从“MyClass”转换为“double”。

Now this error comes from a line in the code where I do this (note that someValueThatIsADouble is of type double):现在这个错误来自我执行此操作的代码中的一行(注意 someValueThatIsADouble 是 double 类型):

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

And the less function is defined this way:而 less function 是这样定义的:

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)?我不明白为什么我会收到这个错误,如果这个错误有充分的理由,为什么我只能在 Debug(而不是在 Release)中得到它? The dll, when compiled in Release, runs fine and doesn't crash. dll 在 Release 中编译时运行良好且不会崩溃。 Also, I checked and there is no #ifdef DEBUG or things like that which could change the code that is compiled in debug and in release.此外,我检查并没有#ifdef DEBUG或类似的东西可以更改在调试和发布中编译的代码。

EDIT:编辑:

I didn't write the code myself and it is an algorithm I don't know much about so I don't know what the double value is supposed to represent and I don't want to go change the logic inside the less function to take a MyClass instead of a double as a second parameter.我自己没有编写代码,这是一个我不太了解的算法,所以我不知道 double 值应该代表什么,我不想 go 将 less function 中的逻辑更改为将 MyClass 而不是 double 作为第二个参数。

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 debug and release implementations of the predicates weren't the same, which made it compile in release and not in debug in my case (because the code isn't very neat and should not use a comparison function with 2 different types). MSalters 的回答表明谓词的调试和发布实现不一样,这使得它在我的情况下在发布而不是在调试中编译(因为代码不是很整洁,不应该使用比较 function 与 2不同种类)。 The hack I did to be able to use this code in debug also was to put this line before any includes (note that the preferred solution should be to have a better comparison function, but in my case it wasn't possible):为了能够在调试中使用此代码,我所做的 hack 也是将此行放在任何包含之前(请注意,首选解决方案应该是更好地比较 function,但在我的情况下这是不可能的):

#define _HAS_ITERATOR_DEBUGGING 0 

The error message suggests you're using MSVC.错误消息表明您正在使用 MSVC。 It's library implementation contains debug checks on predicates.它的库实现包含对谓词的调试检查。 In particular, for a partial order predicate (such as your less ), I think it tests whether Pred(a,b) && Pred(b,a) == false .特别是,对于偏序谓词(例如您的less ),我认为它测试是否Pred(a,b) && Pred(b,a) == false Obviously that won't work here.显然这在这里行不通。

(One of the common errors with predicates is that people used to define an order such that both a<b and b<a . There are many <algorithm> s that break down in that case, which is why there are now some debug checks to prevent such errors. Still they only catch errors if you actually manage to pass a pair of bad a,b values at runtime; they can't catch theoretical errors at compile time) (谓词的一个常见错误是人们习惯于定义一个顺序,使得a<bb<a 。在这种情况下,有许多<algorithm>会崩溃,这就是为什么现在有一些调试检查以防止此类错误。如果您实际上设法在运行时传递了一对错误a,b值,它们仍然只会捕获错误;它们无法在编译时捕获理论错误)

Try with:尝试:

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

first: A forward iterator addressing the position of the first element in the range to be searched.第一个:前向迭代器寻址要搜索的范围内第一个元素的 position。

last: A forward iterator addressing the position one past the final element in the range to be searched. last:前向迭代器寻址 position 一个超过要搜索的范围中的最后一个元素。

val: The value whose first position or possible first position is being searched for in the ordered range. val:在有序范围内搜索其第一个 position 或可能的第一个 position 的值。

comp: User-defined predicate function object that defines sense in which one element is less than another. comp:用户定义的谓词 function object 定义一个元素小于另一个元素的意义。 A binary predicate takes two arguments and returns true when satisfied and false when not satisfied.一个二元谓词需要两个 arguments,满足时返回 true,不满足时返回 false。

It compiles in release because some checks are deactivated in release implementation of std.它在发布时编译,因为在 std 的发布实现中禁用了一些检查。 But it should not compile: val should be of type MyClass and not of type double and then less won't compile anymore even in release.但它不应该编译:val 应该是 MyClass 类型而不是 double 类型,然后 less 即使在发行版中也不会再编译。

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

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