| | |
using(STL)function object (bind2nd) with a user defined function object
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2004
Posts: 1
Reputation:
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:
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
bool operator()(const vertex &a,int b)
C++ Syntax (Toggle Plain Text)
bool operator()(const vertex &a,int b) const
That will let it compile.
-don't listen to me-
![]() |
Similar Threads
- How to make user defined function global? (ColdFusion)
- Can you create a class object based on user input? (C++)
- How to use a user-defined function which generates result set in a SQL statement (MySQL)
- Completing User defined function (C++)
- the object has cv-qualifiers that are not compatible with the member function (C++)
- User-Defined Function - part 2 (C++)
Other Threads in the C++ Forum
- Previous Thread: pointer to member?
- Next Thread: functions
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





