#101 code doesnt compile

closed-invalid
gcc (462)
2002-01-03
2001-12-29
No

Hello,

i've got a problem porting a piece of code from linux
(gcc version egcs-2.90.27 980315 (egcs-1.0.2 release),
yes it is _very_ old) to mingw. The code simply doesnt
compile.

The problem is about using a class method as a
callback.
In my project, i'm using a 'void *' as first argument
to all callbacks thus being able to 'plug' both c and
c++ (by passing class' "this" pointer as first
argument) code to my application.

This is a sample of code which will raise compiling
error:

--->8--->8--->8--->8--->8--->8--->8
#include <stdio.h>

typedef void (*cback)(void *, unsigned int x);

class Test
{
private:
unsigned int me;

public:

Test\(unsigned int me\)
\{
    this->me = me;
\}  
void    Callback\(int x\)
\{
    printf\("Test::Callback\(%d\), me=%d\n",

x, me);
}
};

void TestCallback(cback function, void *ptr,
unsigned int x)
{
function(ptr, x);
}

int main(int argc, char *argv[])
{
Test *t1, *t2;

t1 = new Test\(10\);
t2 = new Test\(20\);

TestCallback\(Test::Callback, t1, 50\);
TestCallback\(Test::Callback, t2, 100\);

return 0;

}

--->8--->8--->8--->8--->8--->8--->8

This compiles nicely on linux (except the
warning "converting from `void (Test::*)(int)' to
`void (*)(void *, unsigned int)") and prints, as
expected:

Test::Callback(50), me=10
Test::Callback(100), me=20

With g++/mingw, i got a couple of lines that seem to
be warnings, but g++ exits with error.

thanks
-- vedad kajtaz

Discussion

  • Danny Smith

    Danny Smith - 2002-01-03
    • status: open --> closed-invalid
     
  • Danny Smith

    Danny Smith - 2002-01-03

    Logged In: YES
    user_id=11494

    It shouldn't compile. The types do not match.

    void Test::Callback(int) does not not match
    type void (*)(void*, unsigned int)'

    and that should throw an error in C++.

    If there is a bug in mingw version of gcc-2.95 it is in
    the reporting of the error. In 3.1 this diagnostic is
    provided:

    bug.cpp: In function `int main(int, char**)':
    bug.cpp:36: no matches converting function `Callback' to
    type `void (*)(void*,
    unsigned int)'
    bug.cpp:17: candidates are: void Test::Callback(int)
    bug.cpp:37: no matches converting function `Callback' to
    type `void (*)(void*,
    unsigned int)'
    bug.cpp:17: candidates are: void Test::Callback(int)

    Danny

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks