www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000361

When Created: 02/01/2003 07:28:33
Against DJGPP version: 2.03
By whom: tapani@programmer.net
Abstract: Problem passing the address of a class member function
I'm not sure if this is a bug as it seems pretty unlikely that everyone would have missed this one so far. When I pass the address of a specific class member function to an appropriate pointer, I get the following error :

object-dependent reference 'function' can only be used in a call to form a pointer to a member function, say '&CLASSDEF::function'


The following code should work (I was told it compiles fine in VC++ 6.0), but gets the same error mentioned above with gcc/DJGPP:

//-------------------------------------


class GBITMAPS;
//forward declaration of class GBITMAPS

class CHAR {
public:
void draw_char(CHAR *p, void (GBITMAPS::*draw)(char, int, int, int, int, int))
{
//function does nothing - it is just a test to see if it accepts parameters without compiler errors
}
};

class GBITMAPS {
public:
void drawskin(char b, int skin, int frame, int x, int y, int a)
{
//function does nothing
}
};


int main()
{
CHAR *chars;
GBITMAPS gfx;
chars = new CHAR[100];
chars[0].draw_char(&chars[0], gfx.drawskin);
return 0;
}

//-------------------------------------

Solution added: 02/02/2003 15:19:50
By whom: rich@phekda.freeserve.co.uk
I did not get the error that the bug poster got. With gcc 3.2.1 I get:

bash-2.04$ !gpp
gpp -g -Wall -o bug361 bug361.c
bug361.c: In function `int main()':
bug361.c:27: no matching function for call to `CHAR::draw_char(CHAR*, <unknown
   type>)'
bug361.c:8: candidates are: void CHAR::draw_char(CHAR*, void
   (GBITMAPS::*)(char, int, int, int, int, int))

Note that the code is not correct C++ according to the ISO C++ standard. The line that passes in a pointer needs to be like this:

chars[0].draw_char(&chars[0], &GBITMAPS::drawskin);

You cannot convert gfx.drawskin into a pointer-to-member-function for non-static member functions.

To use GBITMAPS::drawskin in draw_char, you will need to bind it to an object. So CHAR::draw_char will need to access a GBITMAPS pointer or reference and bind the function to it:

pointer_to_gbitmaps->*draw
ref_to_gbitmaps.*draw

Also, this would not be a bug in DJGPP, if it were a bug. It would be a bug in g++, part of the GNU Compiler Collection.

Fixed in version 2.03 on 02/02/2003 15:20:13
By whom: rich@phekda.freeserve.co.uk



  webmaster     delorie software   privacy  
  Copyright © 2010   by DJ Delorie     Updated Jul 2010