CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2002
    Location
    Germany
    Posts
    451

    Problem with std::map find using MSVC 6.0

    Hi There,

    My code looks like:
    Code:
    class CClassExample {
    	int id1, id2, id3;
    
    	public:
    		CClassExample(int i1, int i2, int i3) {
    			id1 = i1;
    			id2 = i2;
    			id3 = i3;
    		}
    
    		friend bool operator < (const key& left, const key& right) {
    			if (left.id1 > right.id1) {
    				return false;
    			}
    
    			return true;
    		}
    };
    
    typedef std::map<CClassExample, void*> TypeItems;
    static TypeItems Items;
    
    // Putting data into items using insert, it is fine
    size_t st = Items.size(); // It is correct
    
    CClassExample Exa(1, 2, 3); // There is an item having id1 == 1, id2 == 2, id3 ==3
    TypeItems::interator it = Items.find(Exa);
    if (it == Items.end()) {
    // This happens
    }
    Would you please let me know what needs to be done in order that the find could succeed? I tried to overload operator == but that was no help.

    Thanks

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Problem with std::map find using MSVC 6.0

    1) can you provide sample input which demonstrates the problem ?

    2) your operator < is suspect in that if you try to add multiple items
    having the same "id1" value, only the first will actually be inserted.

    Similarly, it should find any object having the same id1 value.

    EDIT:

    I just noticed that operator < references a different class (key).

    How does this compile ?
    Last edited by Philip Nicoletti; September 6th, 2007 at 11:51 AM.

  3. #3
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: Problem with std::map find using MSVC 6.0

    To expand Philips comment - your < operator is wrong. What you have implemented is is <=. Maps explicitly say NOT to do that. Now that you have done this, I suspect that explains your problem - even when map is looking at the item with same id, your '<' operator actually tells it that it is 'not equal' but rather 'less than'.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  4. #4
    Join Date
    May 2002
    Location
    Germany
    Posts
    451

    Re: Problem with std::map find using MSVC 6.0

    Thank you very much. I changed to something like this and then it works fine:
    Code:
    friend bool operator < (const key& left, const key& right) {
    	if (left.id1 < right.id1) {
    		return true;
    	}
    	if (left.id1 == right.id1 && left.id2 < right.id2) {
    		return true;
    	}
    	if (left.id1 == right.id1 && left.id2 == right.id2 && left.id3 < left.id3) {
    		return true;
    	}
    
    	return false;
    }
    I am so happy about it now.

  5. #5
    Join Date
    Jun 2006
    Location
    Germany
    Posts
    103

    Re: Problem with std::map find using MSVC 6.0

    Hello,
    i have a question to this post, i'm confused now!
    Do i need to use friend in overloading operator, i my case i dont have one!

    thanks for any help!
    break;

  6. #6
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: Problem with std::map find using MSVC 6.0

    You do not necessarily need to make the operator as friend. You need to make it a friend only if both of the following cases are true

    1. Your operator (function) is not part of your class.
    2. The operator function needs to access the private members of the classes on which it operates.

    Sometimes you declare the operator to be a member function. Then you do not need to declare it as a friend. 'Effective C++' although has an item devoted on why it might not be a good idea to declare the operators as member functions. If you declare an operator to be a class member then you cannot invoke that operator if the left hand side of an operator is e.g. a constant even if that constant can be promoted to be of the type required for the operator.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  7. #7
    Join Date
    Jun 2006
    Location
    Germany
    Posts
    103

    Re: Problem with std::map find using MSVC 6.0

    Hello,
    thanks for answer, in my case operator is a part of a class, as member, ok
    in this case i dont need this as friend!?
    Code:
    class CTest
    {
    int iage;
    public:
    CTest(int age)
    {
    iage = age;
    }
    bool operator<(const CTest& ctest)
    {
    return iage < ctest.iage;
    }
    bool operator==(const CTest& ctest)
    {
    return iage == ctest.iage;
    }
    }
    Is that what you mean, when is a member of a class i dont need to make it as friend?

    with best regards
    break;

  8. #8
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: Problem with std::map find using MSVC 6.0

    You do not need to make it a friend. That sort of does not even make sense. If a function is part of a class, it is part of the class. It is more than a friend. Real life analogy - if you are part of your family then you are part of it. You do not need to be then a friend of the family e.g. to participate in any of the family stuff.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

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