排序矢量中的值时,会使用排序方法生成错误

当我们在debian机器上使用g ++编译下面的代码时,会生成以下错误...任何人都可以帮助我为什么错误是? 我尝试通过评论排序行然后错误消失,但是我们的任务需要排序完成,那么可能的解决方案是什么

码:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// Here is a simple struct
struct MyStruct
{
   int Num;
   // Define the operator <
   bool operator <(const MyStruct& Rhs)
   {
      return (Num < Rhs.Num);
   }
};

int main()
{
   vector<MyStruct> MyVector;
   // Let the size be 5.
   MyVector.resize(5);
   // Push 5 instances of MyStruct with Num ranging
   // from 5 to 1
   MyStruct TestStruct;
   int i = 0;
   for (i = 0; i < 5; ++i)
   {
      TestStruct.Num = 5 - i;
      MyVector[i] = TestStruct;
   }
   // Now sort the vector
   sort(MyVector.begin(), MyVector.end());
   // Try to display Num for each element. It is sorted
   for (i = 0; i < 5; ++i)
   {
      cout << MyVector[i].Num << 'n';
   }
   return 0;

}

输出:

在/usr/include/c++/4.7/algorithm:63:0包含的文件中,来自testvect.cpp:3:/usr/include/c++/4.7/bits/stl_algo.h:在实例化_RandomAccessIterator std :: __ unguarded_pa​​rtition (_RandomAccessIterator,_RandomAccessIterator,const _Tp&)[with _RandomAccessIterator = __gnu_cxx :: _ normal_iterator>; _Tp = MyStruct]':/usr/include/c++/4.7/bits/stl_algo.h:2309:70:'_RandomAccessIterator std :: __ unguarded_pa​​rtition_pivot(_RandomAccessIterator,_RandomAccessIterator)[with _RandomAccessIterator = __gnu_cxx :: __ normal_iterator]'/ usr / include / c ++ / 4.7 / bits / stl_algo.h:2340:54:需要来自'void std :: __ introsort_loop(_RandomAccessIterator,_RandomAccessIterator,_Size)[with _RandomAccessIterator = __gnu_cxx :: __ normal_iterator>; _Size = int]'/usr/include/c++/4.7/bits/stl_algo.h:5476:4:需要从'void std :: sort(_RAIter,_RAIter)[with _RAIter = __gnu_cxx :: __ normal_iterator>]'testvect。 cpp:33:41:从这里需要/usr/include/c++/4.7/bits/stl_algo.h:2271:4:错误:将'const MyStruct'作为'bool MyStruct :: operator < MyStruct&)'放弃限定符[-fpermissive]


你使用相当过时的编译器,其中stl使用const&参数,在更现代的版本中,这些参数是由rvalue引用传递的,并且不需要const运算符<,因此修复它:

更改:

  bool operator <(const MyStruct& Rhs)

  bool operator <(const MyStruct& Rhs) const
                                       ^^^^^

或者,使用支持更多现代版C ++的编译器的更高版本,然后使用'-std = c ++ 11'或'-std = c ++ 14'启用更新的版本。


更正后的代码:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// Here is a simple struct
struct MyStruct
{
   int Num;
   // Define the operator <
   bool operator <(const MyStruct& Rhs)const
   {
      return (Num < Rhs.Num);
   }
};

int main()
{
   vector<MyStruct> MyVector;
   // Let the size be 5.
   MyVector.resize(5);
   // Push 5 instances of MyStruct with Num ranging
   // from 5 to 1
   MyStruct TestStruct;
   int i = 0;
   for (i = 0; i < 5; ++i)
   {
      TestStruct.Num = 5 - i;
      MyVector[i] = TestStruct;
   }
   // Now sort the vector
   sort(MyVector.begin(), MyVector.end());
   // Try to display Num for each element. It is sorted
   for (i = 0; i < 5; ++i)
   {
      cout << MyVector[i].Num << 'n';
   }
   return 0;
链接地址: http://www.djcxy.com/p/66745.html

上一篇: Error is generated with the sort method while sorting the values inside a vector

下一篇: how to return pair from a function?