This error is quite vague, not sure why this is an error [SOLVED]

New to FreeBASIC? Post your questions here.
Post Reply
Game Coder
Posts: 14
Joined: Apr 17, 2017 2:37

This error is quite vague, not sure why this is an error [SOLVED]

Post by Game Coder »

Hi, so I'm trying to make an events handler and I was doing a test on how the function pointers in freebasic work, I thought that would be a nifty way to accomplish my task. But I don't quite understand why this is a problem. If I am going about this the wrong way, I would like to know the proper way to do this...

Code: Select all

#include "fbgfx.bi"
using FB

type EventHandler
    dim OnSpace as sub
    declare sub Update()
end type

sub EventHandler.Update()
    if multikey(SC_SPACE) then
        this.OnSpace()
    end if
end sub

dim shared Events as EventHandler

type Program
    private:
        dim Text as string = "Space has not been pressed"
    public:
        
        declare sub Main()
        declare sub OnSpace()
end type

sub Program.Main()
    
    do
        cls
        print this.Text
        Events.Update()
    loop until multikey(SC_ESCAPE)
end sub

sub Program.OnSpace()
    this.Text = "Space has now been pressed"
end sub

dim App as Program
Events.OnSpace = @App.OnSpace()
App.Main()
Thank you for your help!
Last edited by Game Coder on Jun 05, 2017 7:54, edited 1 time in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: This error is quite vague, not sure why this is an error

Post by fxm »

FreeBASIC does not support pointers to non-static member procedures.
See for example this topic:
UDT instance member procedure assignement to a member variable
Game Coder
Posts: 14
Joined: Apr 17, 2017 2:37

Re: This error is quite vague, not sure why this is an error

Post by Game Coder »

How would I be able to handle this then? I'd need some kind of workaround for that to have any kind of sensible organization for my library in terms of use...

I also attempted making the sub for OnSpace() static, but again I just get the same error on that line. Does it just not accept it from a UDT at all? The only workarounds I can think of are very poor and seem like very bad code to me, I would greatly appreciate any ideas you had.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: This error is quite vague, not sure why this is an error

Post by fxm »

A workaround to your target code, but that becomes a little twisted.

Your "wished" pointer to a non static member procedure is transposed to a pointer to a static member procedure with one parameter to pass (a 'Program' instance parameter to replace the 'this' hidden instance parameter of a non static member procedure):

Code: Select all

#include "fbgfx.bi"
using FB

type _Program as Program

type EventHandler
    dim OnSpace as sub(byref as _Program)
    declare sub Update(byref p as _Program)
end type

dim shared Events as EventHandler

type Program
    private:
        dim Text as string = "Space has not been pressed"
    public:
       
        declare sub Main()
        declare static sub OnSpace(byref p as Program)
end type

sub Program.Main()
   
    do
        cls
        print this.Text
        Events.Update(this)
    loop until multikey(SC_ESCAPE)
end sub

static sub Program.OnSpace(byref p as Program)
    p.Text = "Space has now been pressed"
end sub

sub EventHandler.Update(byref p as Program)
    if multikey(SC_SPACE) then
        this.OnSpace(p)
    end if
end sub

dim App as Program
Events.OnSpace = @Program.OnSpace
App.Main()
Last edited by fxm on Jun 05, 2017 9:02, edited 5 times in total.
Game Coder
Posts: 14
Joined: Apr 17, 2017 2:37

Re: This error is quite vague, not sure why this is an error [SOLVED]

Post by Game Coder »

Ah ok... That is incredibly helpful, thank you. I can defiantly use that for what I need. Thanks!
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: This error is quite vague, not sure why this is an error [SOLVED]

Post by fxm »

Using inheritance avoids forward declaration of type alias ('type _Program as Program') as in the previous workaround.

Code: Select all

#include "fbgfx.bi"
using FB

type EventHandler
    protected:
        dim OnSpace as sub(byref as EventHandler)
        declare sub Update(byref as EventHandler)
end type

sub EventHandler.Update(byref e as EventHandler)
    if multikey(SC_SPACE) then
        if this.OnSpace <> 0 then
            this.OnSpace(e)
        end if
    end if
end sub

type Program extends EventHandler
    private:
        dim Text as string = "Space has not been pressed"
    public:
        declare constructor(byval as any ptr)
        declare sub Main()
        declare static sub OnSpace(byref as Program)
end type

constructor Program(byval p as any ptr)
    base.OnSpace = p
end constructor

sub Program.Main()
    do
        cls
        print this.Text
        this.Update(this)
    loop until multikey(SC_ESCAPE)
end sub

static sub Program.OnSpace(byref p as Program)
    p.Text = "Space has now been pressed"
end sub

dim App as Program = @Program.OnSpace
App.Main()
Post Reply