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

Recommended Answers

All 2 Replies

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:

template<class _Fn2,
	class _Ty> inline
	binder2nd<_Fn2> bind2nd(const _Fn2& _Func, const _Ty& _Right)
	{	// return a binder2nd functor adapter
	typename _Fn2::second_argument_type _Val(_Right);
	return (std::binder2nd<_Fn2>(_Func, _Val));
	}

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

bool operator()(const vertex &a,int b)

to

bool operator()(const vertex &a,int b) const

That will let it compile.

Heh, in all technicality... Could I feasibly get into trouble for posting snippets of MSVS's non redistributable code? heh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.