943,576 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11480
  • C++ RSS
Aug 11th, 2004
0

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

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drdregambit is offline Offline
1 posts
since Aug 2004
Aug 12th, 2004
0

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

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:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. bool operator()(const vertex &a,int b)
to
C++ Syntax (Toggle Plain Text)
  1. bool operator()(const vertex &a,int b) const

That will let it compile.
Reputation Points: 13
Solved Threads: 2
Newbie Poster
glSuccinct is offline Offline
13 posts
since Aug 2004
Aug 13th, 2004
0

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

Heh, in all technicality... Could I feasibly get into trouble for posting snippets of MSVS's non redistributable code? heh
Reputation Points: 13
Solved Threads: 2
Newbie Poster
glSuccinct is offline Offline
13 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: pointer to member?
Next Thread in C++ Forum Timeline: functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC