Thread: Array of Pointers to Functions in Structs...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up Array of Pointers to Functions in Structs...

    The title sums up my problem. I'm getting all sorts of errors, some saying that '=' doesn't work and undeclared identifiers, etc... I googled it and tried it some other ways but nothing seems to be working... here's what I've tried:

    Code:
    struct TEST
    {
    	void FunctionA();
    	void FunctionB();
    
    	void (*Point[2])(void); // Array Function Pointer
    };
    
    int main()
    {
    	TEST test;
    
    	test.Point[0] = &TEST::FunctionA; // This doesn't work
    	test.Point[1] = test.FunctionB; // Same with this
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You can't assign a non static method to a function pointer. You have to use member pointers.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Change this:
    Code:
    void (*Point[2])(void);
    to:
    Code:
    void (TEST::*Point[2])(void);
    Then use the &TEST::FunctionA syntax. That said, why do you want to do this? There may be better ways, after all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Ah, I see... thanks alot.

  5. #5
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Argh, I got another problem. This time I'm trying to access the function. Why doesn't this work?

    Code:
    test.*Point[1]();
    I get an error saying "'Point' : undeclared identifier".

    Thanks for the quick replys, too.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use the same syntax you used to assign it.

    The () at the end makes it into a function call.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    If you mean by doing this:

    Code:
    test.Point[1]();
    It doesn't work. I get this error for the line:

    Term does not evaluate to a function taking 0 arguments

    Thanks in advance.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Okay, this is my (simplified) code that generates the error:

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct TEST
    {
    	void (TEST::*point[2])();
    	void funca(){cout << "A" ;};
    	void funcb(){cout << "B" ;};
    } test;
    
    int main()
    {
    	test.point[0] = &TEST::funca;
    	test.point[1] = &TEST::funcb;
    
    	test.point[0](); // ***ERROR HERE***
    	test.point[1](); // ***ERROR HERE***
    	cin.get();
    
        return 0;
    }
    Code:
    error C2064: term does not evaluate to a function taking 0 arguments
    Thanks for the help!

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In this case, gcc's error message may give you a push:
    Code:
    error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘test.TEST::point[0] (...)’
    Edit: It may also help to note that test.point[0] is the pointer-to-member-function -- so you'll actually have to call it as:
    Code:
    (test.*(test.point[0]))();
    In other words, now that you have that pointer, you'll have to still use it on the right-hand side of a .* operator.
    Last edited by tabstop; 12-21-2008 at 05:30 PM.

  11. #11
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Ah, yes, that got it working. Thankyou very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. array of structs initialization - PLZ help...
    By Vanya in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2002, 08:10 PM
  5. Array of pointers help
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 02-14-2002, 12:37 PM