0

I have these two classes and I want to sort the list but it shows a lot of errors what is the problem?

    class Especie {

       private:

           string gen;
           string id;
           map<string,int> k_meros;

        public:

         string Especie::consultar_id() const{

        return id;

   } 

        class Cjt_especies {

         private:

              list<Especie> cjt_especies;

              Taula tab;/*ANOTHER CLASS*/

               public:


              bool comp (const Especie& ex,const Especie& ex2){
               return (ex.consultar_id() < ex2.consultar_id());
              }

             void Cjt_especies::something(){

               /*code that makes some push_back at the list*/

               sort(cjt_especies.begin(),cjt_especies.end(),comp);
              }

Why does it not compile? I want to sort the list of Cjt_especies increasingly by id. Thanks.

0xh3xa
  • 4,801
  • 2
  • 14
  • 28
gritaman
  • 3
  • 4

2 Answers2

0

With my not so recent and possibly bad C++ knowledge

std::sort does not seem to work on list (You can opt for a vector instead)

or

std::list has a built in sort

#include <iostream>
#include <map>
#include <algorithm>
#include <list>
#include <vector>

using namespace std;

class Especie 
{

private:

    string gen;
    string id;
    map<string,int> k_meros;

    public:

        string consultar_id() const{
            return id;
        } 
};




class Cjt_especies {

    private:

        list<Especie> cjt_especies;

        // Taula tab;/*ANOTHER CLASS*/

    public:

        Cjt_especies() 
        {
            // this->cjt_especies = new list<Especie>();
        }

        static bool comp(const Especie& ex, const Especie& ex2)
        {
            return (ex.consultar_id() < ex2.consultar_id());
        }

        void something()
        {
            /*code that makes some push_back at the list*/
            this->cjt_especies.sort(Cjt_especies::comp);
        }
};
Supun De Silva
  • 1,437
  • 9
  • 15
0

The comparison function must be static, otherwise if the function is a class member you must instanciate an object of that class in order to use one of it's members.

static bool comp(const Especie &ex, const Especie &ex2) {...}

And you can use list::sort:

cjt_especies.sort(comp);

std::sort is not possible in a list because it requires random-access iterators, lists only have bidirectional iterators, they can only go back or forth one node at a time.

You code has some other problems, I don't know if these are transcript errors or coding errors, nevertheless I leave you the corrected code:

class Especie
{
private:
    string gen;
    string id;
    map<string, int> k_meros;

public:
    string consultar_id() const //extra qualification Especie::
    {                           //is for definition outside the class
        return id;
    }
};

class Cjt_especies
{
private:
    list<Especie> cjt_especies;

public:
    static bool comp(const Especie &ex, const Especie &ex2)
    {
        return (ex.consultar_id() < ex2.consultar_id());
    }

    void something()
    {
        /*code that makes some push_back at the list*/
        cjt_especies.sort(comp);
    }
};

Running code here

On a side note, you should avoid using namespace std;.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • if I use the list::sort it says this /usr/include/c++/7/bits/list.tcc:392:22: error: no match for ‘operator<’ (operand types are ‘Especie’ and ‘Especie’) if (*__first2 < *__first1) ^ – gritaman Apr 30 '20 at 01:54
  • @gritaman, I adapted the answer to use list::sort, you can see the running code in the link. – anastaciu Apr 30 '20 at 02:05