Sorting a vector of objects

This is a discussion on Sorting a vector of objects within the C++ Programming forums, part of the General Programming Boards category; I'm implementing kruskal's algorithm and ran into some troubles compiling. I need to sort a vector of objects by value. ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    Sorting a vector of objects

    I'm implementing kruskal's algorithm and ran into some troubles compiling. I need to sort a vector of objects by value. Here is my code and the error I'm getting.

    Code:
    //These are the two functions in graph.cpp (there are more but are unrelated)
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    #include "graph.h"
    #include "edge.h"
    
    using std::vector;
    
    void graph::sort_edgesArray()
    {
      sort (edgesArray.begin(), edgesArray.end(), graph::cmp_val);   <== This is line 39 in the error
    }
    
    bool graph::cmp_val (edge& a, edge& B)
    {
      return (a.val < b.val);
    }
    Code:
    //This is the header file graph.h
    #ifndef GRAPH_H
    #define GRAPH_H
    
    #include <vector>
    #include "edge.h"
    
    using std::vector;
    
    class graph
    {
     public:
      
      int nEdges;
      int nVerts;
      vector<edge> edgesArray;            //array that holds edges
      //might need a vertices vector
    
      graph() :
        nEdges(0),
        nVerts(0)
        {}
    
      void addVert(int vert);
        
      void addEdge(int start, int end, int value);
    
      int get_edges();
    
      int get_verts();
    
      void sort_edgesArray();
    
      bool cmp_val(edge& a, edge& b);
    };
    
    #endif
    Code:
    //This is the edge.h header file where graph.cpp is calling from to construct the vector.
    #ifndef EDGE_H
    #define EDGE_H
    
    //Edges class in the graph
    class edge
    {
     public:
      
      int node1;
      int node2;
      int val;
    
      //constructor
     edge(int start, int end, int value) :
        node1(start),
        node2(end),
        val(value)
        {}
        
    };  //end class edge
    
    #endif
    Code:
    //This is the error I'm getting.
    graph.cpp: In member function ‘void graph::sort_edgesArray()’:
    graph.cpp:39:33: error: no matching function for call to ‘sort(std::vector<edge>::iterator&, std::vector<edge>::iterator&, <unresolved overloaded function type>)’
    /usr/include/c++/4.5/bits/stl_algo.h:5236:18: note: candidate is: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<edge*, std::vector<edge> >, _Compare = bool (graph::*)(edge&, edge&)]
    Last edited by edishuman; 02-11-2012 at 01:17 AM.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Make the cmp_val function static or declare it as a non member function.

    You also have some const issues. Change cmp_val to take const references.
    Last edited by manasij7479; 02-11-2012 at 02:09 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    Quote Originally Posted by manasij7479 View Post
    Make the cmp_val function static or declare it as a non member function.
    I'm curious as to why the function needs to be static / non-member. I'm looking at the c++ reference page and the comparison function seems to work fine.

    Also curious as to why we need it to be const?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    The syntax graph::cmp_val is valid for static functions, otherwise member functions have to be called with an object. I'm not sure what you saw on your reference but it's probably not what you think.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    Read up about crossposting.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism via a vector of objects
    By Bozebo in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2010, 05:46 AM
  2. Vector of pointers to vector of objects
    By Litz in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2009, 02:29 PM
  3. vector of function objects
    By idleman in forum C++ Programming
    Replies: 6
    Last Post: 09-11-2009, 11:59 AM
  4. maximum objects in vector
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 07-21-2002, 12:49 PM
  5. sorting objects
    By blight2c in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2002, 02:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21