C++ (error: passing ‘const …'’ as ‘this’ argument of ‘…’ discards qualifiers [duplicate])

Ubuntu下编译GCC编译caffe,出现error

c++/4.8/bits/stl_algo.h:2245:19:     error: passing const yolo_v2::DATA as  this  argument of  bool yolo_v2::DATA::operator<(yolo_v2::DATA)  discards qualifiers [-fpermissive]
    while (__pivot < *__last)

大致的意思就是:将非变量传递的类型不一致导致错误

作者代码:

 bool operator<( DATA data)
        {
            return this->confidence > data.confidence;
        }

定位到STL的源码:

/// This is a helper function...
  template
    _RandomAccessIterator
    __unguarded_partition(_RandomAccessIterator __first,
			  _RandomAccessIterator __last, const _Tp& __pivot)
    {
      while (true)
	{
	  while (*__first < __pivot)
	    ++__first;
	  --__last;
	  while (__pivot < *__last)
	    --__last;
	  if (!(__first < __last))
	    return __first;
	  std::iter_swap(__first, __last);
	  ++__first;
	}
    }
解决方案:(链接:https://stackoverflow.com/questions/19073996/error-passing-const-as-this-argument-of-discards-qualifiers)

bool operator<(const DATA& data) const
        {
            return this->confidence > data.confidence;
        }


你可能感兴趣的:(C++)