Define custom comparison

This is a discussion on Define custom comparison within the C++ Programming forums, part of the General Programming Boards category; Hello fellow programmers. This question should be easy, but already wasted hours of googling and testing I now turn to ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Define custom comparison

    Hello fellow programmers.

    This question should be easy, but already wasted hours of googling and testing I now turn to you. As a part of a project I am using a vector < vector <double > > data structure and want to sort the "outer" vector by comparing the "inner" vectors. For my case this makes sense and should be done simply by comparing the first two numbers (doubles).

    I am getting a "error C4430: missing type specifier - int assumed" error amongst others when trying to compile. I have tested and identified the following part of the code as the problem. Also a syntax error missing ")" before "<" on my custom comparison.

    Code:
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    //First define the sorting - getting the 
    bool SortOnFirstElement(const < vector<double> &u,const vector<double> &v)
            {
                if(u[0]< v[0]) return true;
                else return false;
            }
    int main()
    {
    //lot of other code that was written and tested before the custom comparison
    //came into play
    
        vector < vector<double> > ratio;
        vector < vector<double> >* pratio =&ratio;
    
    //Then some initialization of the ratio vector. I have checked it is 
    //intitalized correctly by printing out
    
    
        //Now sort
        sort((*pratio).begin(),(*pratio).end(), SortOnFirstElement);
    
    }
    I know I probably should use -> instead of the (*). If more code is needed please ask. The problem is almost certaintly in the part defining the comparison. For the interested I can say the method will eventually solve instances of the multiple-choice knapsack problem.

    Thank you all in advance for your time and effort!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    At a glance, it looks like a typo error here:
    Code:
    bool SortOnFirstElement(const < vector<double> &u,const vector<double> &v)
    i.e., it should be:
    Code:
    bool SortOnFirstElement(const vector<double> &u, const vector<double> &v)
    Out of curiosity, but why do you need pratio when ration appears to be in the same scope?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Doh!

    The code is now compilling. I guess there is no reason I use pointers for the vector, hence I should probably not use it. Thanks for that point!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing flags from a DLL?
    By RobotGymnast in forum C++ Programming
    Replies: 17
    Last Post: 10-27-2008, 01:34 PM
  2. Bor to DevC++ prog convert prob
    By kryptkat in forum Windows Programming
    Replies: 16
    Last Post: 09-18-2007, 05:11 AM
  3. Error check problem again
    By rtransfi in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 03:55 PM
  4. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 08:44 AM
  5. keyboard control
    By MathFan in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2002, 11:55 AM

Tags for this Thread


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