Pointer to function won't

This is a discussion on Pointer to function won't within the C++ Programming forums, part of the General Programming Boards category; Point to a function, that is. I have a class called viewPort, which is a simple 2D raster graphics class. ...

  1. #1
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368

    Pointer to function won't

    Point to a function, that is.

    I have a class called viewPort, which is a simple 2D raster graphics class. I want there to be several plotting functions (such as overlay, XOR, blend, etc) so I thought I'd use a function pointer.

    Here is an abridged code portion

    Code:
    class viewPort
    {
    .
    .
    .
    public:
       void plot (int x, int y, int col);
       void (*plotH) (int x, int y, int col);
    };
    
    .
    .
    .
    
    viewPort::viewPort (int width, int height, HWND hwnd)
    {
    .
    .
    .
    plotH = plot;
    .
    .
    .
    }
    The line in bold generates a compile-time error: cannot convert from (__thiscall viewPort::*) (int, int, int) to (__cdecl *) (int, int, int).

    I really don't know what's wrong. I've used function pointers before with no problems - but not with class member functions. I looked function pointers up in C++ Primer and it makes no mention of any exceptions when pointing to member functions.

    As far as I can see, this is a non-error. It's a pointer in viewPort to a void (int, int, int) and I'm trying to point it to a void (int, int, int) which is also in the viewPort class.

    And to add insult to injury, when I try to do what it wants and typecast to (__cdecl *) it says it doesn't recognise __cdecl DESPITE it changing colour when I type it.

    I'm totally stuck.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    We did this about ten hours ago.

  3. #3
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    ......... Sorry.

    Do I need the class:: prefix?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Does adding the &, as mentioned in the thread, work for you?

  5. #5
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    I'm on my friend's machine at the moment, but I'll try both these things.

    I've never had to use the & operator in this situation before (but, like I said, I've never tried it with member functions and I use a notoriously non-standard compiler - MS VC++ 6).

  6. #6
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    OK so I tried these things and it didn't work.

    Now it's:
    Code:
    void (viewPort::*plotH) (int x, int y, DWORD col);
    	void plot (int x, int y, DWORD col);
    
    plotH = &viewPort::plot;
    And for every invocation through the pointer it says "term does not evaluate to a function" - regardless as to whether I use the & operator to assign the pointer.

    Then I tried adding "const = 0" then it just said pure virtual specifier can only be used for functions.
    MSVC++ 6.0

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This works for me:
    Code:
    class bob {
    	bob();
    	int x, y, z;
    	static int func(int a, int b, int c);
    	int (*funcp)(int a, int b, int c);
    };
    
    int bob::func(int a, int b, int c) {
    	return a*b+c;
    }
    
    bob::bob() {
    	x = 3;
    	y = 4;
    	z = 5;
    	funcp = &bob::func;
    }
    I believe that there is no such thing as a pointer to a non-static member function, except perhaps using the ever-popular .* or ->* notation, but I'm not finding that in the standard right this minute.

    Edit: Alright, it's a pointer to member thing. This is the example from the standard:
    Quote Originally Posted by ISO C++, section 8.3.3
    [Example:
    Code:
    class X {
    public:
        void f(int);
        int a;
    };
    class Y;
    int X::* pmi = &X::a;
    void (X::* pmf)(int) = &X::f;
    double X::* pmd;
    char Y::* pmc;
    declares pmi, pmf, pmd and pmc to be a pointer to a member of X of type int, a pointer to a member of X
    of type void(int), a pointer to a member of X of type double and a pointer to a member of Y of type
    char respectively. The declaration of pmd is well-formed even though X has no members of type
    double. Similarly, the declaration of pmc is well-formed even though Y is an incomplete type. pmi and
    pmf can be used like this:
    Code:
    X obj;
    //...
    obj.*pmi = 7; // assign 7 to an integer member of obj
    (obj.*pmf)(7); //call a function member of obj with the argument 7
    —end example]
    Last edited by tabstop; 09-01-2008 at 09:28 PM.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Perhaps try a more up-to-date compiler before we start blaming the compiler and look for workarounds? There are several free ones out there, you know, including VS 2008 Express, a much much more standards compliant compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If it makes you feel better, I was getting the same errors using, oddly enough, VS 2008 Express. So I figured maybe it was a real error.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    This works for me:
    Code:
    class bob
    {
    	bob();
    	int x, y, z;
    	int func(int a, int b, int c); // Removed static
    	int (bob::* funcp)(int a, int b, int c);
    };
    
    int bob::func(int a, int b, int c)
    {
    	return a * b + c;
    }
    
    bob::bob()
    {
    	x = 3;
    	y = 4;
    	z = 5;
    	funcp = &bob::func;
    	(this->*funcp)(1, 2, 3);
    }
    
    int main()
    {
    
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by samGwilliam View Post
    I have a class called viewPort, which is a simple 2D raster graphics class. I want there to be several plotting functions (such as overlay, XOR, blend, etc) so I thought I'd use a function pointer.
    Can I make a suggestion: Instead of having a pointer to a function, have a pointer to a class (a "baseplot class", which has pure virtual memberfunction(s) to do the actual plotting) - not only does it make the syntax much easier to handle, but it's also "the correct way to solve this type of problem in an Object Oriented Design).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Thank you. I want to get it to work as a function pointer just on principle more than anything but I like the plot class idea too.
    MSVC++ 6.0

  13. #13
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    OK, so Elysia's solution worked but I've got to say the anality of the syntax seems unnecessary to me.

    I'm sure Stroustroup had his reasons though...
    MSVC++ 6.0

  14. #14
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    There's another problem now.

    I've fixed the problem with the viewPort class, now I want a derived class (called vSpec) to be able to provide its own plotting method.

    Code:
    plotH = &vSpec::aPlot;
    This generates:

    Code:
    error C2440: '=' : cannot convert from 'void (__thiscall vSpec::*)(int,int,unsigned long)' to 'void (__thiscall viewPort::*)(int,int,unsigned long)'
    Which is understandable, so I tried to typecast with:

    Code:
    plotH = (viewPort::*) &vSpec::aPlot;
    And it then says:

    Code:
    error C2059: syntax error : '<tag>::*'
    To do what I want must be possible right? Am I typecasting wrongly?
    MSVC++ 6.0

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by samGwilliam View Post
    To do what I want must be possible right?
    It depends on what you mean by "what I want". If you mean make this function pointer point to that function, then the answer is no, you can't; a member function of viewPort and a member function of vSpec are not compatible types. That is, there is no way to make their parameter list match, since the implicit this parameters are of different types.

    If you mean override this function call, then that's what virtual functions are born to do.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 08:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21