简体   繁体   English

在类内按数组中的属性对对象进行排序,这些对象本身就是另一个类的对象

[英]sorting objects by attribute in an array within a class which are themselves objects of another class

I am trying to sort an array of objects which are stored within another object and then display the sorted objects. 我试图对存储在另一个对象中的对象数组进行排序,然后显示排序后的对象。 I want to sort these objects by a specific attribute. 我想按特定属性对这些对象进行排序。 I'm not exactly sure how I am supposed to run the sort() method. 我不确定我应该如何运行sort()方法。

composer.h 作曲家

class Composer {
  Public:
    int get_ranking();
    ..
    ..
};

database.h 数据库

Class Database {
  Public:
    void DisplayAll(){
      for(int i=0;i<next_slot_;i++){
            composers_[i].Display();
      }
    };

    bool SortByRank(Composer const & a, Composer const & b) {
        return a.get_ranking() < b.get_ranking();
    };

    void DisplayByRank(){
      sort(composers_, composers_+next_slot_, SortByRank);
      DisplayAll();
    };

testdatabase.cpp testdatabase.cpp

int main(){
  Database myDB;

  Composer& comp1 = myDB.AddComposer("Ludwig van", "Beethoven", "Romantic", 1770,
                   "Beethoven was completely deaf during the latter part of his life - he 
                    never heard a performance of his 9th symphony.");
  comp1.Promote(7);

  Composer& comp2 = myDB.AddComposer("Johann Sebastian", "Bach", "Baroque", 1685,
                   "Bach had 20 children, several of whom became famous musicians as well.");
  comp2.Promote(5);

  cout << endl << "all Composers: " << endl << endl;
  myDB.DisplayAll();
  myDB.DisplayByRank();

When I run this I get: 当我运行它时,我得到:

error: passing 'const Composer' as 'this' argument of 'int Composer::get_ranking()' discards qualifiers [-fpermissive] return a.get_ranking() < b.get_ranking(); 错误:将“ const Composer”作为“ int Composer :: get_ranking()”的“ this”参数传递会丢弃限定符[-fpermissive] return a.get_ranking()<b.get_ranking();

and

error: no matching function for call to 'sort(Composer [100], Composer*, unresolved overloaded function type)' sort(composers_, composers_+next_slot_, SortByRank); 错误:没有匹配函数可用于调用“ sort(Composer [100],Composer *,未解决的重载函数类型)” sort(composers_,composers_ + next_slot_,SortByRank);

Adding const to the end of get_ranking() seems to have solved the const correctness error. 将const添加到get_ranking()的末尾似乎已经解决了const正确性错误。

You should add const to get_ranking method. 您应该将const添加到get_ranking方法。

class Composer {
public:
    int get_ranking() const;
};

Which is part of C++ const-correctness. 这是C ++常数正确性的一部分。 It means that the method does not change internal state of the object (except mutable fields which are rather rarely used) it is called by and allows to call this method by const objects. 这意味着该方法不会更改对象的内部状态(除了很少使用的mutable字段之外),它可以被const对象调用并允许调用此方法。

Alright, so I did a fair amount of reading and learned a bit. 好吧,所以我做了很多阅读并学到了一些东西。

After resolving my const correctness by changing int get_ranking(); 通过更改int get_ranking();解决了我的const正确性之后int get_ranking(); to int get_ranking()const it turned out my SortByRank() still wasn't working. int get_ranking()const ,结果我的SortByRank()仍然无法正常工作。 This was because it was a non-static member of Database requiring a this pointer. 这是因为它是需要this指针的数据库的非静态成员。 Simply changing it to: static bool SortByRank(); 只需将其更改为: static bool SortByRank(); or defining SortByRank() outside of the Database class solves the issue. 或在Database类之外定义SortByRank()即可解决此问题。

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

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