using(STL)function object (bind2nd) with a user defined function object

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2004
Posts: 1
Reputation: drdregambit is an unknown quantity at this point 
Solved Threads: 0
drdregambit drdregambit is offline Offline
Newbie Poster

using(STL)function object (bind2nd) with a user defined function object

 
0
  #1
Aug 11th, 2004
struct vertex //object in the list
{
int data ;
list <edge*> edges ;
};

list <vertex> thenodes ;
//user defined function object
struct checking : public binary_function <vertex,int,bool>
{
bool operator()(const vertex &a,int b)
{
return (a.data == b) ;
}

};
//the function using the find_if function
list <vertex>::iterator findv(int thing)
{
list <vertex>::iterator result ;
result = find_if(thenodes.begin(),thenodes.end(),bind2nd(checking(),thing)) ;
return result ; //the problem
}
this code is from a graph code , the problem is not the graph, but the using of the user defined functor checking() with the bind2nd() function in the find_if function , the microsoft compiler give me an error of :
cannot convert 'this' pointer from 'const struct graph::checking' to 'struct graph::checking &' // so what is the problem ???
note : the problem may be easy coz i am just a beginer
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 13
Reputation: glSuccinct is an unknown quantity at this point 
Solved Threads: 2
glSuccinct's Avatar
glSuccinct glSuccinct is offline Offline
Newbie Poster

Re: using(STL)function object (bind2nd) with a user defined function object

 
0
  #2
Aug 12th, 2004
The problem: C++ doesn't allow you to call non-const methods on const objects.

Deep in the MSVS.NET 2k3 source, I found the declartion of bind2nd:
  1. template<class _Fn2,
  2. class _Ty> inline
  3. binder2nd<_Fn2> bind2nd(const _Fn2& _Func, const _Ty& _Right)
  4. { // return a binder2nd functor adapter
  5. typename _Fn2::second_argument_type _Val(_Right);
  6. return (std::binder2nd<_Fn2>(_Func, _Val));
  7. }

The problem is in this definition, the first paramter is a reference to const. Anything deeper in the call stack from there will believe it's operating on a const object. What's that have to do w/ anything? bool checking::operator() is NOT a const method.

Make checking::operator() const.

Change
  1. bool operator()(const vertex &a,int b)
to
  1. bool operator()(const vertex &a,int b) const

That will let it compile.
-don't listen to me-
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 13
Reputation: glSuccinct is an unknown quantity at this point 
Solved Threads: 2
glSuccinct's Avatar
glSuccinct glSuccinct is offline Offline
Newbie Poster

Re: using(STL)function object (bind2nd) with a user defined function object

 
0
  #3
Aug 13th, 2004
Heh, in all technicality... Could I feasibly get into trouble for posting snippets of MSVS's non redistributable code? heh
-don't listen to me-
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC