Thread: Define custom comparison

  1. #1
    Registered User
    Join Date
    Oct 2009

    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.

    #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
    At a glance, it looks like a typo error here:
    bool SortOnFirstElement(const < vector<double> &u,const vector<double> &v)
    i.e., it should be:
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009

    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, 02:34 PM
  2. Bor to DevC++ prog convert prob
    By kryptkat in forum Windows Programming
    Replies: 16
    Last Post: 09-18-2007, 06:11 AM
  3. Error check problem again
    By rtransfi in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 04:55 PM
  4. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 09:44 AM
  5. keyboard control
    By MathFan in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2002, 12:55 PM

Tags for this Thread

Website Security Test