Hi all,
I've spent the last week trying to sort this out, and the more I delve into it, the more confused I'm getting. I've done some background research (searched on google and here for sort function issues) and really I can't make head or tail of what's going on.
My question is quite simple. Why does the following not work?
Let's say I have a class Foo with two members:
#include "random.h"
class Foo
{
public:
double fooMember1;
double fooMember2;
Random rand;
Foo(){};
~Foo(){};
void setFooMemberValues()
{
//random value generated with a random generator
fooMember1 = rand.getRandomNumber();
//random value generated with a random generator
fooMember2 = rand.getRandomNumber();
};
};
Now let's say I have another class called Foo2:
#include "foo.h"
#include <vector>
#include <algorithm>
class Foo2
{
public:
std::vector<Foo*> fooObjectPointers;
void run()
{
createFoo();
sort(fooObjectPointers.begin(), fooObjectPointers.end(), fooSort);
destroyAllFoo();
};
void createFoo()
{
for( int i=0; i<10; i++ )
fooObjectPointers.push_back( new Foo );
};
void destroyAllFoo()
{
for( unsigned int i=0; i<fooObjectPointers.size(); i++ )
delete fooObjectPointers[i];
};
bool fooSort(Foo* fooPointer1, Foo* fooPointer2)
{
return fooPointer1->fooMember1 < fooPointer2->fooMember2;
};
};
This gives me the following compiler error:
:foo2.cpp:20: error: no matching function for call to 'sort(__gnu_cxx::__normal_iterator<Foo**, std::vector<Foo*, std::allocator<Foo*> > >, __gnu_cxx::__normal_iterator<Foo**, std::vector<Foo*, std::allocator<Foo*> > >, <unknown type>)'
So I googled this, and discovered that it doesn't work because the comparison function fooSort is non-static (this all works fine if I just declare the function separate from the class Foo2 and do the sorting in main()).
I didn't really and still don't understand what it means and the more I delved into it the more I came across things I didn't understand (like functors).
I found this workaround in the end: http://bytes.com/topic/c/answers/525611-problems-stl-sorting-using-member-functions.
When I employed this at first, it seemed everything was working fine (no idea why, but ok it seemed to be working). Then I decided to check for sure and I printed out the values of fooMember1 in each of the objects pointed to by the entries in the vector fooObjectPointers. I discovered to my surprise that the values of fooMember1 had completely changed! Yes, they were now all sorted correctly, but somehow the values were totally different! What's more, the values of fooMember2 were also completely different.
Somehow it seems as though new objects of Foo were instantiated through the sorting procedure, by which fooMember1 and fooMember2 acquired new random values. The sorting proceeded with these values and NOT the original values that I wanted to sort.
Then I decided that maybe it would be easiest to just program my own sort function, so I went ahead and did this. I discovered to my amazement that the same thing was happening. In particular, the following assignment seems to be doing something nuts in my code (nuts to me at least):
std::vector<Foo*> fooObjectPointers1;
std::vector<Foo*> fooObjectPointers2;
fooObjectPointers2 = fooObjectPointer1;
Here, fooObjectPointers1 points to objects that I created earlier using the createFoo() function. fooObjectPointers2 has yet to be filled. When I did the "=" assignment in main() and I print out the vectors, the memory addresses are exactly the same, and indeed they point to the same objects when I print out the member variables of Foo. When I do this "=" assignment within the Foo2 class, fooObjectPointers2 has a different set of memory addresses, and points to totally different values of the member variables of Foo!!!
Also, to complicate my question more, why can I not do this?
std::vector<Foo*> fooObjectPointers1;
std::vector<Foo*> fooObjectPointers2;
fooObjectPointers2[0] = fooObjectPointer1[0];
Program builds fine but crashes when I run it when I do that.
I'm guessing I'm doing something very stupid and basic, but I just can't figure it out.
I'd appreciate any and all help. This is something I really need to sort out at short notice because I can proceed with the rest of my code.
Thanks in advance,
Kartik