My goal was to sort a list of outputted vectors by length. I ended up trying to do this by creating a simple class that stored the vector, and the length of the vector. I then overloaded the < operator so that I could use the STL sort function. I had all kind of problems. I was able to make them go away when I stopped using dynamic allocation (dynamic allocation was used so that the vectors were d-dimensional, I then fixed their dimension at 3). I have attached the code that uses dynamic allocation (note that I have fixed the dimension at 3 for simplicity), and hence doesn't work. All that's required to get my code to work is to change the statement in dBallMember.h from int* vector to int vector[3] and removing all new and delete statements as appropriate. I just don't understand what I haven't done right. Help?
Re: Difficulties Using STL sort on Array of Objects
oh PS if you actually want to try and compile this, there is another function called increment that you will need, I didn't post it because it along with its header file exceeded the 5 file limit, but I can post it on request. Increment just moves along from one d-dimensional vector with a maximum length of R to the next in a methodical fashion so that all of them are swept over.
Re: Difficulties Using STL sort on Array of Objects
First quick look.
Code:
int* vector;
Vector is a bad name for a variable as the STL already contains a template std::vector.
You will find your code easier to get right if you try using std::vector instead of arrays (dynamic or not).
Also using vector means you don't have to write your on assignment operator (Note: your class was missing a copy constructor)
Last edited by JohnW@Wessex; July 28th, 2008 at 03:30 AM.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Re: Difficulties Using STL sort on Array of Objects
It was actually the copy constructor that was the problem... to be honest I'm largely self taught in the actual syntax of C++ (the courses I've had focused on algorithms in-class, so the instructor never really talked about syntax) and I didn't know what a copy constructor was until I was talking with a friend about this and they happened to mention a copy constructor... I was in shock (and then furious since I've wasted 6+ hours playing around with various new and delete statements in my code even though nothing was wrong with it). Thanks for your help though, I appreciate it!
Re: Difficulties Using STL sort on Array of Objects
Originally Posted by Nirf
I was in shock (and then furious since I've wasted 6+ hours playing around with various new and delete statements in my code even though nothing was wrong with it). Thanks for your help though, I appreciate it!
So my suggestion would be to minimize/not use new and delete and if you really have to, there are smart pointers to leverage which also help in class copy constructor and assignment operators.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.