Thread: sort with vector pointers

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

    Question sort with vector pointers

    Code:
    bool MySort(CModel* entity1 , CModel* entity2)
    {
            return entity1->m_pDiffuseMap < entity2->m_pDiffuseMap;
    
    }
    
    
    
    std::sort (m_modelvector.begin(), m_modelvector.end(), MySort);
    m_modelvector is
    vector <CModel*> m_modelvector;


    im getting these errors


    : error C3867: 'CRenderManager::MySort': function call missing argument list; use '&CRenderManager::MySort' to create a pointer to member

    : error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided

    i know its some thing stupid , but just cant figure it out , any one help please?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm guessing that you declared MySort as a member function of CModel. It may make more sense to declare it as a non-member function, or maybe a static member function. (And rename it, since it compares, not sorts.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Hey look, the same answer you got yesterday. If you don't understand the response, perhaps you should ask for clarification rather than jumping from forum to forum hoping someone will answer it in simpler terms.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    19
    tryed making a it a static and got exact same thing

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thedodgeruk
    tryed making a it a static and got exact same thing
    On the other hand, I tried making it a static member function and did not get the same thing:
    Code:
    #include <vector>
    #include <algorithm>
    
    class CModel
    {
    public:
        CModel() : m_pDiffuseMap(0) {}
    
        static bool MySort(CModel* entity1 , CModel* entity2)
        {
            return entity1->m_pDiffuseMap < entity2->m_pDiffuseMap;
        }
    private:
        int m_pDiffuseMap;
    };
    
    int main()
    {
        std::vector<CModel*> m_modelvector(100);
        std::sort(m_modelvector.begin(), m_modelvector.end(), CModel::MySort);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Laser, are you sure there should not be a & before the function in your example?
    As in,
    &CModel::MySort
    instead of
    CModel::MySort
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    Laser, are you sure there should not be a & before the function in your example?
    As in,
    &CModel::MySort
    instead of
    CModel::MySort
    It's optional. You don't need & when taking the address of a function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But it's different with a member function.
    EDIT: I suppose it's different with static member functions. VC compiles without the ampersand.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    19
    cheers , it looks like is the same thing as templates , dont seam to like it when you put them in the cpp file

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should not put templates in .cpp files. Write them inline and store them in headers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by thedodgeruk View Post
    cheers , it looks like is the same thing as templates , dont seam to like it when you put them in the cpp file
    One can put class static methods in a cpp file just fine.
    You must have gotten the syntax wrong.
    Post the code you tried and we'll show you what you did wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sort a std::vector<int>
    By Coding in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2008, 02:43 PM
  2. sort a vector
    By Coding in forum C++ Programming
    Replies: 6
    Last Post: 02-21-2008, 10:22 AM
  3. Sort 2D Vector
    By DaveHope in forum C++ Programming
    Replies: 11
    Last Post: 02-14-2008, 03:24 AM
  4. sort a vector
    By mickey0 in forum C++ Programming
    Replies: 4
    Last Post: 07-15-2007, 04:19 PM
  5. vector sort
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 10-05-2005, 10:28 AM