Results 1 to 10 of 10

Thread: Function Pointers HELP!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    123

    Question Function Pointers HELP!

    I am trying to mess around with a kind of circular logic between two classes using a function pointer. VS 6 is not cooperating or I am using improper syntax. Can someone give me an idea what is wrong?? thanks

    Code:
    #ifndef ABC_H
    #define ABC_H
    
    #include <iostream.h>
    #include "xyz.h"
    
    
    class Abc 
    {	
    	
    	public:
    	
    		int truefalse(void);
    		void call_xyz(void);
    
    	private:
    
    		Xyz obj_xyz;
    };
    
    #endif
    
    #include "abc.h"
    #include "xyz.h"
    
    int Abc::truefalse(void)
    {
    	int int_rc;
    	
    	cout << "Enter a 1 or 0: ";
    	cin >> int_rc;
    	cout << endl;
    
    	return int_rc;
    }
    
    void Abc::call_xyz()
    {
    	obj_xyz.what_you_picked(truefalse);	
    }
    
    #include "abc.h"
    
    void main()
    {
    	Abc obj_abc;
    
    	obj_abc.call_xyz();
    }
    
    #ifndef XYZ_H
    #define XYZ_H
    
    #include <iostream.h>
    
    class Xyz
    {
    	public:
    	
    		void what_you_picked(int (*truefalse)(void));
    
    };
    
    #endif
    
    #include "xyz.h"
    
    void Xyz::what_you_picked(int (*truefalse)(void))
    {
    	if((*truefalse)())
    	{
    		cout << "You Picked One" << endl;
    
    	}
    
    	if(!(*truefalse)())
    	{
    		cout << "You Picked Zero" << endl;
    	}
    
    }
    thats the code and here is the error i get:

    C:\Windows\Desktop\software eng\test\abc.cpp(17) : error C2664: 'what_you_picked' : cannot convert parameter 1 from 'int (void)' to 'int (__cdecl *)(void)'
    None of the functions with this name in scope match the target type

  2. #2

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    123

    experts!

    someone here has gotta know this, help me out

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Pointers to class member functions are complicated. Are you sure you're ready for it?

    There was a thread a while ago, you might want to search for it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    123
    i searched "function pointers" and came up with nothing...do you have the address on hand???

    thank you

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    http://www.vbforums.com/showthread.p...mplate+pointer

    Here it is, it's with templates so it's more complicated than what you're doing, but you may be able to get the info you need.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    123
    it definately gave me some good info, but im not sure if i can directly apply it to what i am trying to do.

    in that example, the function pointer and class address are being passed through main, but in mine i need the Xyz class to call the function truefalse in Abc, to do this it seems it would need knowledge of the class Abc to recieve the parameters into the what_you_picked function. but i dont know how to give Xyz knowledge of class Abc.

    am i making any sense here? hmmm

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The problem is that a class member function
    void abc::dosomething(int i)
    {
    m_i = i * 5;
    }

    is this for the compiler:
    void dosomething(abc *this, int i)
    {
    this->m_i = i * 5;
    }

    So the compiler converts these calls:
    pABC->dosomething(5);
    abc_obj.dosomething(5);
    to
    dosomething(pABC, 5);
    dosomething(&abc_obj, 5);

    So the compiler always needs a class object to call any class member functions. When you have a pointer to a member function of class abc, you call it like this:
    pABC->*pFunc(params);
    And the typedef of the pointer is
    typedef void (abc::*FUNC_PTR)(int);

    So to make your program work:
    Code:
    #ifndef XYZ_H
    #define XYZ_H
    
    #include <iostream.h>
    
    class abc;  // forward declaration
    
    class Xyz
    {
    	public:
    	
    		void what_you_picked(int (abc::*truefalse)(void), abc* pabc);
    
    };
    
    #endif
    
    #include "xyz.h"
    
    void Xyz::what_you_picked(int (abc::*truefalse)(void), abc* pabc)
    {
    	if((pabc->*truefalse)())
    	{
    		cout << "You Picked One" << endl;
    
    	}
    
    	if(!(pabc->*truefalse)())
    	{
    		cout << "You Picked Zero" << endl;
    	}
    
    }
    
    void Abc::call_xyz()
    {
    	obj_xyz.what_you_picked(truefalse, this);	
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    123

    so close

    now it will compile and runs part way, but it gives a debug error as call_xyz returns...


    something about the ESP not being properly saved across a function call, and not using the proper calling convention.


    i have no idea what this might be???

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    use
    int (_cdecl abc::*truefalse)(void)

    It's about calling conventions - who is responsible for clearing the parameters and such.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    if that doesn't work, use
    int (_stdcall abc::*truefalse)(void)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width