dimanche 5 mars 2023

"No matching function for call to sort"

Hi guys im trying to learn how to code in c++, the problem, today i tried to write a small part of a program but there is an error that kept showing up which is : "No matching function for call to sort" Spend lot of time searching on internet but im still blocked, and don't really know what to do... It's looks like it's the sort who is bugging but i don't why

I tried to add static in front of the bool, but nothing change, i also tried to change "vector<Particule>&" in the prototype to : auto& particules but it also didn't work. Oh yea also on internet they told me to add #include <algorithm> so "sort" could work but it's seems to be useless hen i add it Here is my code :

#include <iostream>

#include <fstream>

#include <vector>

#include <algorithm>

using namespace std;

class Particule;
struct S2d {
    int x;
    int y;
};

//PROTOTYPES
vector<Particule> chargerParticules(const string& nom_fichier);
static bool compareParticules(std::vector<Particule>& particules);
void trierParticules(vector<Particule>& particules);

class Particule {

public:
    Particule(S2d pos, double d_p)
        : position(pos)
        , d_particule(d_p)
        , isActive(true){};
    double get_d_particule() const { return d_particule; }

private:
    S2d position; //Its a structure form with x and y inside
    double d_particule;
    bool isActive;
};

int main(int argc, const char* argv[])
{
    string nom_fichier = "nom_du_fichier.txt";
    vector<Particule> particules = chargerParticules(nom_fichier);
    trierParticules(particules);

    return 0;
}

vector<Particule> chargerParticules(const string& nom_fichier)
{
    ifstream fichier(nom_fichier);
    if (!fichier) {
        cout << "Erreur impossible de trouver le fichier" << nom_fichier << endl;
        exit(1);
    }

    int nbP; //decla nombres de particules
    fichier >> nbP; //lit contenu fichier "file" et stocker sa valeur dans variable "nbP"
    vector<Particule> particules;
    for (int i = 0; i < nbP; ++i) {
        S2d pos;
        double d_p;
        fichier >> pos.x >> pos.y >> d_p;
        /*lit 3 valeurs depuis file et les mets dans
                pos.x, pos.y et d_p*/
        particules.emplace_back(pos, d_p);
    }
    return particules;
}

static bool compareParticules(const Particule& p1,
    const Particule& p2)
{
    return p1.get_d_particule() > p2.get_d_particule();
}

void trierParticules(vector<Particule>& particules)
{
    sort(particules.begin(), particules.end(), compareParticules);
}

This is the error:

<source>: In function 'void trierParticules(std::vector<Particule>&)':
<source>:76:9: error: no matching function for call to 'sort(std::vector<Particule>::iterator, std::vector<Particule>::iterator, <unresolved overloaded function type>)'
   76 |     sort(particules.begin(), particules.end(), compareParticules);
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-trunk-20230305/include/c++/13.0.1/algorithm:61,
                 from <source>:7:
/opt/compiler-explorer/gcc-trunk-20230305/include/c++/13.0.1/bits/stl_algo.h:4851:5: note: candidate: 'template<class _RAIter> constexpr void std::sort(_RAIter, _RAIter)'
 4851 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~
/opt/compiler-explorer/gcc-trunk-20230305/include/c++/13.0.1/bits/stl_algo.h:4851:5: note:   template argument deduction/substitution failed:
<source>:76:9: note:   candidate expects 2 arguments, 3 provided
   76 |     sort(particules.begin(), particules.end(), compareParticules);
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-trunk-20230305/include/c++/13.0.1/bits/stl_algo.h:4882:5: note: candidate: 'template<class _RAIter, class _Compare> constexpr void std::sort(_RAIter, _RAIter, _Compare)'
 4882 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
      |     ^~~~
/opt/compiler-explorer/gcc-trunk-20230305/include/c++/13.0.1/bits/stl_algo.h:4882:5: note:   template argument deduction/substitution failed:
<source>:76:9: note:   couldn't deduce template parameter '_Compare'
   76 |     sort(particules.begin(), particules.end(), compareParticules);
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-trunk-20230305/include/c++/13.0.1/algorithm:73:
/opt/compiler-explorer/gcc-trunk-20230305/include/c++/13.0.1/pstl/glue_algorithm_defs.h:292:1: note: candidate: 'template<class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::sort(_ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Compare)'
  292 | sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp);
      | ^~~~
/opt/compiler-explorer/gcc-trunk-20230305/include/c++/13.0.1/pstl/glue_algorithm_defs.h:292:1: note:   template argument deduction/substitution failed:
<source>:76:9: note:   candidate expects 4 arguments, 3 provided
   76 |     sort(particules.begin(), particules.end(), compareParticules);
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Aucun commentaire:

Enregistrer un commentaire