Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Ask Function problem?thanks

Ask Function problem?thanks

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rxgmoral
    wrote on last edited by
    #1

    Ask Function problem?thanks <<<<<<<<<>>>>>>>>> #pragma once class Class { public: int get_return(int a,int b,int (*compare)()); int Rxgmoral(int a,int b); void Fun(); }; <<<<<<<<>>>>>>> #include "Stdafx.h" #include "Class.h" int Class::get_return(int a,int b,int(* compare)()) { return (compare(a,b)); } int Class::Rxgmoral(int a, int b) { return a+b; } void Class::Fun() { int a; a=get_return(1,2,&Rxgmoral); } error: e:\mystudio\project\test\test\class.cpp(6) : error C2197: 'int (__cdecl *)(void)' : too many arguments for call e:\mystudio\project\test\test\class.cpp(17) : error C2276: '&' : illegal operation on bound member function expression thanks:)

    V P 2 Replies Last reply
    0
    • R rxgmoral

      Ask Function problem?thanks <<<<<<<<<>>>>>>>>> #pragma once class Class { public: int get_return(int a,int b,int (*compare)()); int Rxgmoral(int a,int b); void Fun(); }; <<<<<<<<>>>>>>> #include "Stdafx.h" #include "Class.h" int Class::get_return(int a,int b,int(* compare)()) { return (compare(a,b)); } int Class::Rxgmoral(int a, int b) { return a+b; } void Class::Fun() { int a; a=get_return(1,2,&Rxgmoral); } error: e:\mystudio\project\test\test\class.cpp(6) : error C2197: 'int (__cdecl *)(void)' : too many arguments for call e:\mystudio\project\test\test\class.cpp(17) : error C2276: '&' : illegal operation on bound member function expression thanks:)

      V Offline
      V Offline
      Viorel
      wrote on last edited by
      #2

      I think you should try this:

      // Class.h
      #pragma once
      class Class
      {
      public:
      	int get_return(int a, int b, int (*compare)(int, int));
      	static int Rxgmoral(int a, int b);
      	void Fun();
      };
      //
      // Class.cpp
      #include "Stdafx.h"
      #include "Class.h"
      int Class::get_return(int a, int b, int(* compare)(int, int))
      {
      	return compare(a, b);
      }
      
      int Class::Rxgmoral(int a, int b)
      {
      	return a + b;
      }
      
      void Class::Fun()
      {
      	int a;
      	a = get_return(1, 2, Rxgmoral);
      }
      

      I hope it helps.

      1 Reply Last reply
      0
      • R rxgmoral

        Ask Function problem?thanks <<<<<<<<<>>>>>>>>> #pragma once class Class { public: int get_return(int a,int b,int (*compare)()); int Rxgmoral(int a,int b); void Fun(); }; <<<<<<<<>>>>>>> #include "Stdafx.h" #include "Class.h" int Class::get_return(int a,int b,int(* compare)()) { return (compare(a,b)); } int Class::Rxgmoral(int a, int b) { return a+b; } void Class::Fun() { int a; a=get_return(1,2,&Rxgmoral); } error: e:\mystudio\project\test\test\class.cpp(6) : error C2197: 'int (__cdecl *)(void)' : too many arguments for call e:\mystudio\project\test\test\class.cpp(17) : error C2276: '&' : illegal operation on bound member function expression thanks:)

        P Offline
        P Offline
        prasad_som
        wrote on last edited by
        #3

        rxgmoral wrote:

        int get_return(int a,int b,int (*compare)());

        get_return has second parameter as 'pointer to a function taking nothing returning int'.

        rxgmoral wrote:

        a=get_return(1,2,&Rxgmoral);

        where as you are trying here to pass address of member function again having different signature. Modified code should look like this

        #pragma once
        class Class
        {
        public:
        int get_return(int a,int b,int (Class::*compare)(int,int));//watch declaration of pointer
        int Rxgmoral(int a,int b);
        void Fun();
        };

        int Class::get_return(int a,int b,int(Class::*compare)(int,int))
        {
        return ((this->*compare)(a,b));
        }

        int Class::Rxgmoral(int a, int b)
        {
        return a+b;
        }

        void Class::Fun()
        {
        int a;
        a=get_return(1,2,Rxgmoral);//no need to pass address(&)
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups