Error C2664 Function Pointer
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this error c2664 cannot convert parameter site About Us Learn more about Stack Overflow the company Business Learn more error c2664 cannot convert parameter 1 from about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x error c2664 in c++ Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How can error c2664 lpcwstr i convert pointer-method of some class to pointer-function? up vote 2 down vote favorite 1 Greating everybody! I have a function-pointer method int Myclass::*myMethod(char* a,char* b){ //some code } And try to run it bool Myclass::myMethod2(){ AnotherClass *instance = AnotherClass:getInstance(); instance-> addParams(&myMethod); return true; } AnotherClass - this class in another dll. AnotherClass definition class AnotherClass { //friend class Myclass; public: static AnotherClass* getInstance();
Error C2664 Cannot Convert Argument
void addParams(int (*myMethod)(char*, char*) =0); //I try so void addParams(int (Myclass::*myMethod)(char*, char*) =0); }; And have error C2664. Cannot convert parameter 1 from 'int Myclass::* (__cdecl *)(char *,char *)' to 'int (__cdecl *)(char *,char *). Hm.. What should i do? c++ pointers callback function-pointers c2664 share|improve this question edited Jul 18 '11 at 10:30 Curt 61.8k36182282 asked Jul 18 '11 at 9:57 Stepchik 1221111 4 It's impossible to convert a member function pointer to a non-member function pointer. What is it that you're really trying to do here? There might be a better way to do what you want. –In silico Jul 18 '11 at 10:02 AnotherClass looks badly-designed, since it takes just a function pointer, with no context belonging to the caller. Your caller apparently wants to provide some context (when calling a non-static member function MyMethod on a particular instance of Myclass, you need something for this to point to). C-style callback interfaces usually have a user data pointer for this, in C++ you can do the same, or use polymorphism. Depending how important the dll boundary is, you could perhaps make addParams a template. &nda
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About
Error C2664 Cannot Convert Parameter From Const Char To Lpcwstr
Us Learn more about Stack Overflow the company Business Learn more about hiring mfc error c2664 developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Visual C++ errors C3867 and C2664 http://stackoverflow.com/questions/6731205/how-can-i-convert-pointer-method-of-some-class-to-pointer-function up vote 1 down vote favorite #define UCHAR unsigned char typedef bool (*FUNC)(UCHAR uc1, UCHAR uc2); typedef void(*PF)(FUNC, UCHAR*); PF Assign; class Class { private: UCHAR buf[32]; bool func(UCHAR c1, UCHAR c2) { } public: Class::Class(void) { Assign( func, buf ); // <<< Microsoft VC++ error C3867 } Class::~Class() { } }; error C3867: 'Class::func': function call missing argument list; use '&Class::func' to create a pointer to http://stackoverflow.com/questions/14501474/visual-c-errors-c3867-and-c2664 member If I try the suggestion in the error message above Assign( &Class::func, buf ); // <<< Microsoft VC++ error C2664 I get this error: error C2664: 'void (FUNC,unsigned char *)' : cannot convert parameter 1 from 'bool (__thiscall Class::* )(unsigned char,unsigned char)' to 'FUNC' There is no context in which this conversion is possible Without changing anything else how can I get this Assign() function to compile? These typedefs are from a library that I need to interface with. c++ class function-pointers share|improve this question edited Jan 24 '13 at 12:37 hmjd 88.7k8115176 asked Jan 24 '13 at 12:33 user292344 78118 Never do this: #define UCHAR unsigned char - always use a typedef, e.g. typedef unsigned char UCHAR; –Paul R Jan 24 '13 at 12:35 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote func is a non-static member function, so its type is bool (Class::*)(UCHAR, UCHAR), while Assign requires bool (*)(UCHAR, UCHAR). You cannot convert a non-static member function to a non-member function. To be able to pass func into Assign, you'll have to make func static. share|improve this answer answered Jan 24 '13 at 12:36 Angew 93.9k
function like this void MouseButton(int button, int state, int x, int y); ... glutMouseFunc(MouseButton); But now I would like to pass a member function instead of a "real" (non-member? whats the word for this?) function. class Plot https://www.daniweb.com/programming/software-development/threads/144313/passing-a-member-function-pointer { void Plot::MouseButton(int button, int state, int x, int y); ... Plot() { glutMouseFunc(MouseButton); https://social.msdn.microsoft.com/Forums/en-US/07baa0da-85d4-4335-93b8-8822101c5573/error-2-error-c2664-problem-involve-class-function-pointerhelp-help?forum=vssmartdevicesnative } } But I get this error: /home/dave/Plot/src/Plot.h:81: error: argument of type 'void (Plot::)(int, int, int, int)' does not match 'void (*)(int, int, int, int)' What do I have to do differently to get this to work? Thanks! Dave daviddoria 334 1,968 posts since Feb 2008 Community Member Featured 9Contributors 22Replies 24Views 8 YearsDiscussion Span 8 Years error c2664 Ago Last Post by kux 0 Narue 5,707 8 Years Ago Assuming you have control over glutMouseFunc, you need to overload it to accept a pointer to a member function: class Plot { public: void MouseButton(); }; void glutMouseFunc ( void (Plot::*pmf)() ) { //... Plot p; (p.*pmf)(); //... } 0 Discussion Starter daviddoria 334 8 Years Ago I actually don't have control over glutMouseFunc (I mean I suppose I could edit error c2664 cannot the glut source, but that sounds like a bad idea) Why is it so much different to pass a member function than a normal function? Is there no way to do this without overloading glutMouseFunc? Thanks! Dave 0 Alex Edwards 321 8 Years Ago I actually don't have control over glutMouseFunc (I mean I suppose I could edit the glut source, but that sounds like a bad idea) Why is it so much different to pass a member function than a normal function? Is there no way to do this without overloading glutMouseFunc? Thanks! Dave You could just overload the function to accept a pointer-to-member function. 0 Discussion Starter daviddoria 334 8 Years Ago As I mentioned, I can't overload the function because it is not mine, it is in the glut library! 0 ArkM 1,090 8 Years Ago Non-static member function always called for the object, not in itself. The glutMouseFunc does not know, which object used for member function call. So it's no sense to pass a member function pointer to glutMouseFunc: it can't use such a pointer. In other words, a pointer to a function is an address but a pointer to a member function is (roughly speaking) an offset in a class member functions table. The gkutMouseFunc w
SQL Server 2014 Express resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. Ask a question Quick access Forums home Browse forums users FAQ Search related threads Remove From My Forums Answered by: Error 2 error C2664: Problem involve class Function pointer.Help Help... Smart Device Development > Visual Studio Smart Device Development - Native C++ Project Question 0 Sign in to vote Anyone can help to solve this problem? Almost 2 weeks stuck at this problem... Below is the error message: 1.Situation A Error 2 error C2664: 'MyClass2::Function2' : cannot convert parameter 1 from 'void (__thiscall MyClass1::* )(int,int)' to 'type_myCallBack' 2. Situation B Error 2 error C3867: 'MyClass1::Function3': function call missing argument list; use '&MyClass1::Function3' to create a pointer to member typedef void (*type_myCallBack) (int object, int event);
type_myCallBack mycall; class MyClass2 { void MyClass2 :: Function2(type_myCallBack my_arg) { mycall= my_arg; } void MyClass2 :: run(void) { if (mycall != 0) mycall((int) this, 1); } }; class MyClass1 { void MyClass1 :: MyCheck(int object) { //assume some code below... // // } void MyClass1 :: Function3(int object, int ievent) { switch(ievent) { case 1: MyCheck(object); break; case 2: MyCheck(object); break; } } void MyClass1 :: Function1(void) { MyClass2 * func= new MyClass2(); if (func!=0) { func->Function2(&MyClass1::Function3); // <-Situation A error line... func->Function2(Function3); // <-Situation B error line... } } }; Tuesday, May 13, 2008 8:43 AM Reply | Quote Answers 0 Sign in to vote I am not sure at all that this kind of callback construct is ever going to work. Wel