CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2012
    Posts
    3

    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);
    }
    
    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&)]

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Sorting a vector of objects

    Quote Originally Posted by edishuman View Post
    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.
    See http://www.parashift.com/c++-faq-lit....html#faq-33.3.
    You either need to make the function cmp_val a static member function or (better) make it a global function (possibly in an anonymous namespace). You can also overload operator < for the edge class, such that you can omit the third argument to std::sort.

    Some other notes:
    - Don't put a using declaration in the global scope of a header file. This effectively makes namespaces useless.
    - You don't need to store the number of edges, since it is already given by edgesArray.size().
    - Don't make data members of a class public. Make them private and define public member functions that do whatever the class needs to do. If you need to, provide accessor functions to a member variable.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 2008
    Posts
    725

    Re: Sorting a vector of objects

    bool graph::cmp_val (edge& a, edge& B)

    why pass by reference? Should be value or const ref.

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Sorting a vector of objects

    Another idea would be to just implement your operator<, if it makes sense.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured