Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对用户创建对象的向量进行排序(使用类)_C++_Stdvector - Fatal编程技术网

C++ 对用户创建对象的向量进行排序(使用类)

C++ 对用户创建对象的向量进行排序(使用类),c++,stdvector,C++,Stdvector,到目前为止,我看到的所有其他问题和答案都使用struct作为对象。我的代码是 class Thing { private: string name; double balance; public: string getName(){return name;} double getBalance(){return balance;} }; bool sortByName(const Thing &lower, const Thing &upper) {return

到目前为止,我看到的所有其他问题和答案都使用struct作为对象。我的代码是

class Thing
{
private:
  string name;
  double balance;
public:
  string getName(){return name;}
  double getBalance(){return balance;}
};

bool sortByName(const Thing &lower, const Thing &upper)
{return (lower.getName() < upper.getName());}

bool sortByBalance(const Thing &lower, const Thing &upper)
{return (lower.getBalance() < upper.getBalance());}

int main()
{
  vector<Thing> myvec
  //Code to add objects with attributes

  sort(myvec.begin(), myvec.end(), sortByName);
  sort(myvec.begin(), myvec.end(), sortByBalance);
  //Code to display rearranged vector
getName和getBalance将返回对象的名称,它们是类中的私有变量

如何比较来自不同类的两个值,然后在sort函数中实现它们

*编辑:我在开头添加了Thing类的定义

您应该将getName和getBalance声明为const函数

class Person
{
  ...
  const string& getName() const { return name; }
}

您当前的代码有什么问题?我收到的错误消息是main。cpp:16:24:error:将“const Person”作为“this”参数传递会丢弃限定符[-fppermissive]{return lower.getName