Sorting STL Vector of Pointers (Split)

This is a discussion on Sorting STL Vector of Pointers (Split) within the C++ Programming forums, part of the General Programming Boards category; [Mod note: split from here: http://cboard.cprogramming.com/cplus...pointers.html] Hi! I know that this topi is very old but I will try anyway... ...

1. 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"

Giulia

2. 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.

3. 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. 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?

5. 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. 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.

7. Ok I didn't know I could do like this!
thank you very much for the help, now it works, finally!