•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,983 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,731 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 7032 | Replies: 2
![]() |
•
•
Join Date: Aug 2004
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
{
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
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:
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
to
That will let it compile.
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)
bool operator()(const vertex &a,int b) const
That will let it compile.
-don't listen to me-
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- Can we call a User defined Function using Linked Servers ? (MS SQL)
- User-defined functions (C++)
- User-Defined Function - part 2 (C++)
- restore default (built-in) function after overriding (HTML and CSS)
- How can I create a user defined function (MS SQL)
- error in user defined string class (C++)
Other Threads in the C++ Forum
- Previous Thread: pointer to member?
- Next Thread: functions


Linear Mode