Click here to Skip to main content
15,849,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I have a class node(name, vector, distance, prev) and i want to sort it by distance. And i have used some methods like sort or with a operator but nothing seems to work. Is there any other way to to sort a vector of classes?

What I have tried:

I also used
sort( test.begin(), test.end(), [](const node &a, const node &b){ return (a.distanceToStart < b.distanceToStart);});


but i get a lambda error codes.
error: no match for call to '(main()::<lambda(const node&,="" const="" node&)="">) (node*&, node*&)'
158 | { return bool(_M_comp(*__it1, *__it2)); }


node a = node("a",{}, 15, &a); node b = node("b",{}, 14, &a); node c = node("c",{}, 25, &a); node d = node("d", {}, 1, &a); node e = node("e", {}, INT_MAX, &a);


#include <iostream>
#include <vector>
using namespace std;

class edge;


class node{

public: 
    string name;
    node* prev;
    vector<edge*> edges;
    int distanceToStart;

    node(string name, vector<edge*> edges, int distanceToStart, node* prev){
        this ->distanceToStart = distanceToStart;
        this ->edges = edges;
        this ->name = name;
        this ->prev = prev;
    }

    int getDistanceToStart(){
        return distanceToStart;
    }

    void setDistanceToStart(int d){
        distanceToStart = d;
    }

};
Posted
Updated 28-Jun-23 6:35am
v2
Comments
Quinten Bakker (QB) 28-Jun-23 9:48am    
Forget the erro code
error: no match for call to '(main()::<lambda(const node&,="" const="" node&)="">) (node*&, node*&)'
158 | { return bool(_M_comp(*__it1, *__it2)); }
Patrice T 28-Jun-23 10:13am    
Use Improve question to update it.
Your comment will get more attention if inside the question.

I added a main function the above code vis:
C++
// .. QB's code up to here 
#include <algorithm>

int main()
{
    vector<node> test;
    sort( test.begin(), test.end(), [](const node &a, const node &b){ return (a.distanceToStart < b.distanceToStart);});
}

This compiles without error using GCC, Clang and MSVC. Either you've managed to correct your lambda, or there is something else in your code that's causing problems. Maybe start by removing the using namespace std; directive and explicitly use the std namespace as needed.

If you're using separate compilation of your classes, then you should not be using namespace std; in the class header file. Doing so means that anything that does an #include "myclass.hpp" will also import all of the std namespace, which might cause unexpected results.
 
Share this answer
 
Comments
Quinten Bakker (QB) 28-Jun-23 10:41am    
I change my code to be with out using std but i stil get the same errro code. Can it be because i am using pointer en ref?
k5054 28-Jun-23 11:42am    
I don't know what you mean. But see Richard's comment about the code that's generates the error. He and I are basically saying the same thing. What you have posted here seems to be correct. So there's something else that's causing the error. Are you including the declaration for class node in the file for main? Have you inadvertently redefined what a node is? Maybe something else....
I have simplified your node class and the following code builds and runs using Microsoft C++ compiler:
C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

class node{
public: 
    std::string name;
    int distanceToStart;

    node(std::string name, int distanceToStart)
    {
        this ->distanceToStart = distanceToStart;
        this ->name = name;
    }

    int getDistanceToStart(){
        return distanceToStart;
    }
};

int main()
{
    node a("a", 15);
    node b("b", 14);
    node c("c", 25);
    std::vector<node> test;
    test.push_back(a);
    test.push_back(b);
    test.push_back(c);

    std::sort( test.begin(), test.end(), [](const node &a, const node &b){ return (a.distanceToStart < b.distanceToStart);});
    for (auto n : test)
    {
        std::cout << "Node " << n.name << " " << n.distanceToStart << std::endl;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900