CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2008
    Posts
    17

    Difficulties Using STL sort on Array of Objects

    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?
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2008
    Posts
    17

    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.

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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

  4. #4
    Join Date
    Jul 2008
    Posts
    17

    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!

  5. #5
    Join Date
    May 2007
    Posts
    811

    Re: Difficulties Using STL sort on Array of Objects

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

    Here is a good start.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured