Thread: Sorting STL Vector of Pointers (Split)

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Sorting STL Vector of Pointers (Split)

    [Mod note: split from here: Sorting an STL Vector of pointers

    Hi! I know that this topi is very old but I will try anyway...
    I had exactly the same problem with the sorting of a vector of pointer to objects. But my situation is a bit different:

    I have a the files Function.cc and Function.hh that define the object Function. I call this object in a main file and there I fill a vector with Function*. Now I want to sort the vector in this main file.
    If i define the comparing struct in the main file before int main{} (so as a function) it works perfectly... but can I define it inside the Fuction.cc (or .hh) file and then call it for the sort in the main file? At the momenti I just get "Compare is not defined in main.cpp"


    thanks in advace!

    Giulia
    Last edited by CornedBee; 11-19-2010 at 06:58 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Hi, and welcome. We have a board policy of not reviving old threads, so please don't. In the future, if you have a question that is very similar to an existing one, create a new thread and post a link to the old one there.

    Yes, you can define the comparator in Function.hh. main.cpp must be able to see it, so it can't be in Function.cc.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Thanks for the quick answer! And sorry, next time I will open a new thread...

    Anyway I still haven't solved the problem...
    I'm using this structure

    Code:
    struct Compare {
    	bool operator ()(Function *f1, Function *f2) { return *f1 < *f2; }
    };
    and the operator
    Code:
    bool Function::operator<(Function& f1)
    {
    	bool b;
    	b= this->get_value() < (f1.get_value());
    	return b;
    }
    As before, putting the structure in main.cpp just before int main{} it works perfectly...but if I write it only in Function.hh when i perform the sort (std::sort(v.begin(),v.end(),Compare())) i get the error "Compare was not declared in this scope". I think I miss something that I shohld write, but I don't know where..

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Both Compare::operator() and Function::operator< should be marked const, but that's not directly related to your problem.

    It sounds like it should work. Perhaps you can post reduced versions of your files that show the problem?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    I also think it should work!
    Yes, of course, here they are...
    I just noticed that I forgot something: Fuction is designed as an abstract class (then of "type" Function I have FLineare, FExponential...). Maybe this is the problem, I still don't have very clear what i can do with abstract classes..
    Code:
    #ifndef Funzione_HH
    #define Funzione_HH
    #include "Dato.hh"
    #include <vector>
    #include <iostream>
    using namespace std;
    class Function
    {
    	protected:
    		vector<Dato> a;
    		double chi;
    	public:
    		Function();
    		Function(vector<Dato> coeff,double c);
    		virtual ~Function(){};
    	
    		//METODI;
    		virtual double YValue(double x)=0;
    		vector<Dato> Coefficients();
    		double get_value();
    		bool operator<(Function f1);
    
    	struct Compare {
    		bool operator ()(Function *f1, Function *f2) { return *f1 < *f2; }
    	};
    	
    };
    # endif
    Code:
    #include "Function.hh"
    using namespace std;
    Function::Function()
    {}
    Function::Function(vector<Dato> coeff,double c)
    {
    	a=coeff;
    	chi=c;
    }
    vector<Dato> Function::Coefficients()
    {
    	return a;
    }
    double Function::get_value()
    {
    	return chi;
    }
    
    
    bool Function::operator<(Funzione& f1)
    {
    	bool b;
    	b= this->get_value() < (f1.get_value());
    	return b;
    }
    And in the end the main
    Code:
    int main{
    
    vector<Function*> functions;
    
    // calculations to fill the vector..
    
    std::sort(functions.begin(),functions.end(),Compare());
    
    }
    The only error i get when i compile is "main.cpp: Compare was not declared in this scope"..

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You've put Compare inside the class Funzione. So either put it outside (but with this name, that's a bad idea), or qualify it as Funzione::Compare.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Ok I didn't know I could do like this!
    thank you very much for the help, now it works, finally!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  2. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM
  3. Sorting STL vector
    By TonyP in forum C++ Programming
    Replies: 7
    Last Post: 02-10-2003, 04:38 PM
  4. Map Container & Sorting :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2002, 06:01 PM
  5. STL list::sort() sorting structs
    By hk_mp5kpdw in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2002, 07:23 AM