vector find error


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: vector find error

  1. #1
    Join Date
    Oct 2010
    Posts
    119

    vector find error

    Hello everyone

    could you please look at the error generated by the below piece of code? It is apparently generated by the IF statement inside the for loop because commenting this IF statement resolves the error.

    Code:
    for ( std::vector<TRS_block>::iterator trs = common_TRS.begin() ; trs < common_TRS.end(); trs++ )
    	{
                    // error occurs because of this IF statement
    		if(std::find( trs_file_as_blocks.begin(), trs_file_as_blocks.end(), *trs ) != trs_file_as_blocks.end())
    		{
    				
    		}
    	}
    and the error is:

    Code:
    /usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../include/c++/4.4.4/bits/stl_algo.h:174: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = mergeTRSLCdata::TRS_block*, _Container = std::vector<mergeTRSLCdata::TRS_block, std::allocator<mergeTRSLCdata::TRS_block> >]() == __val’
    Please suggest how to resolve this issue.

    Thanks

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    Guessing here, because that is pretty convoluted, but it looks like the == operator that we discussed earlier was not visible to the compiler (???) or, if this is another class/project, it boils down to the same thing. Your code is calling the == function behind the scenes and the compiler cannot find an overloaded == function that will work, you need to either supply one (if you have not) or make it visible (there is a design error preventing it from being used) to the compiler.

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    Just a shot in the dark: try replacing std::vector<TRS_block>::iterator trs
    with
    std::vector<TRS_block>::const_iterator trs.
    Danny Kalev

  4. #4
    Join Date
    Oct 2010
    Posts
    119
    Thanks Jonnin and Danny :). Overloading the ==() operator has solved my problem

    Code:
    friend bool operator==(const TRS_block& lhs, const TRS_block& rhs)
    {
      return (lhs.TOTALS        == rhs.TOTALS) &&
    	 (lhs.TRS_O_SLIP_NO == rhs.TRS_O_SLIP_NO) &&
    	 (lhs.raw_contents  == rhs.raw_contents);
    }

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    Great! and I'm glad you used the extern friend option. Your code demonstrates again why it's important to overload symmetric binary operators as external functions, not as member functions.
    Danny Kalev

  6. #6
    Join Date
    Oct 2010
    Posts
    119
    :)

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2009, 09:22 AM
  2. Replies: 5
    Last Post: 01-17-2007, 01:37 PM
  3. 'On Error Goto' versus 'Try, Catch, Fail'
    By clarence_rollins in forum .NET
    Replies: 21
    Last Post: 09-11-2002, 11:32 AM
  4. Returning errors from SQL Server Stored Procs
    By Khalizan in forum VB Classic
    Replies: 1
    Last Post: 11-28-2001, 01:32 AM
  5. Replies: 0
    Last Post: 04-07-2000, 09:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center