error 215: Only static members can be accessed from static functions

General FreeBASIC programming questions.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

error 215: Only static members can be accessed from static functions

Post by mrminecrafttnt »

I've planned to make a database with id auto incrementation but this idea failed because only static members are allowed :/

Code: Select all

type test
    redim as string datatable(any)
    id as integer
    declare sub adddata (s as string,byref d as integer = id)
    declare sub listdata
end type

sub test.adddata (s as string,byref d as integer = id)
    d+=1
    redim preserve datatable(id)
    datatable(id-1)=s
    end sub

sub test.listdata
    for i as integer = lbound(datatable) to ubound(datatable)
        print i,datatable(i)
    next
end sub

dim as test t
t.adddata ("HELLO")
t.adddata ("WORLD")

t.listdata
sleep
end
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: error 215: Only static members can be accessed from static functions

Post by fxm »

id auto incrementation:

Code: Select all

type test
    redim as string datatable(any)
    id as integer
    declare sub adddata (s as string)
    declare sub listdata
end type

sub test.adddata (s as string)
    redim preserve datatable(id)
    datatable(id) = s
    id += 1
end sub

sub test.listdata
    for i as integer = lbound(datatable) to ubound(datatable)
        print i,datatable(i)
    next
end sub

dim as test t
t.adddata ("HELLO")
t.adddata ("WORLD")

t.listdata
sleep

If the user also wants to be able to coerce the id, you need to add an overloaded member procedure:

Code: Select all

type test
    redim as string datatable(any)
    id as integer
    declare sub adddata (s as string)
    declare sub adddata (s as string, d as uinteger)
    declare sub listdata
end type

sub test.adddata (s as string)
    redim preserve datatable(id)
    datatable(id) = s
    id += 1
end sub

sub test.adddata (s as string, d as uinteger)
    if d >= id then
        redim preserve datatable(d)
        id = d + 1
    end if
    datatable(d) = s
end sub

sub test.listdata
    for i as integer = lbound(datatable) to ubound(datatable)
        print i,datatable(i)
    next
end sub

dim as test t
t.adddata ("HELLO")
t.adddata ("WORLD")
t.adddata ("CONTINUE", 3)
t.adddata ("END")

t.listdata
sleep
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: error 215: Only static members can be accessed from static functions

Post by fxm »

@Jeff,

Just a quick note on this error message:
215 Only static members can be accessed from static functions

In the code below, this type of error message is well suited to the static test1() procedure member body, but much less so to the non-static test2() procedure member declaration:

Code: Select all

Type UDT
    Dim As Integer I
    Declare Static Sub test1()
    Declare Sub test2(Byval J As Integer = I)
End Type

Sub UDT.test1()
    I = 1
End Sub

Sub UDT.test2(Byval J As Integer)
    I = J
End Sub
Compiler output:
C:\.....\FBIDETEMP.bas(4) error 215: Only static members can be accessed from static functions, found 'I' in 'Declare Sub test2(Byval J As Integer = I)'
C:\.....\FBIDETEMP.bas(8) error 215: Only static members can be accessed from static functions, found 'I' in 'I = 1'
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: error 215: Only static members can be accessed from static functions

Post by coderJeff »

In both cases the error is due to:
1) symbol is implicitly identified as a member. i.e. it is known that 'I' is a member of 'UDT', but
2) instance pointer, i.e. the hidden 'this' parameter, is not available / allowed for the context.

Maybe:
Only static members can be accessed from static functions and parameter initializers
?
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: error 215: Only static members can be accessed from static functions

Post by fxm »

coderJeff wrote: Feb 24, 2023 13:39 Maybe:
Only static members can be accessed from static functions and parameter initializers
?
Yes, it's much better.
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: error 215: Only static members can be accessed from static functions

Post by fxm »

Version without using a 'id' member but just the UBound of array:

Code: Select all

type test
    redim as string datatable(any)
    declare sub adddata (s as string)
    declare sub adddata (s as string, d as uinteger)
    declare sub listdata
end type

sub test.adddata (s as string)
    redim preserve datatable(ubound(datatable) + 1)
    datatable(ubound(datatable)) = s
end sub

sub test.adddata (s as string, d as uinteger)
    if d > ubound(datatable) then
        redim preserve datatable(d)
    end if
    datatable(d) = s
end sub

sub test.listdata
    for i as integer = lbound(datatable) to ubound(datatable)
        print i,datatable(i)
    next
end sub

dim as test t
t.adddata ("HELLO")
t.adddata ("WORLD")
t.adddata ("CONTINUE", 3)
t.adddata ("END")

t.listdata
sleep
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: error 215: Only static members can be accessed from static functions

Post by coderJeff »

fxm wrote: Feb 24, 2023 13:59
coderJeff wrote: Feb 24, 2023 13:39 Maybe:
Only static members can be accessed from static functions and parameter initializers
?
Yes, it's much better.
This error message is updated in latest fbc/master (fbc 1.10.0)
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: error 215: Only static members can be accessed from static functions

Post by SARG »

@fxm
As St_W's build not yet made I upload a new lastest version. :wink:
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: error 215: Only static members can be accessed from static functions

Post by fxm »

THANKS.
I will also add the new inc/fbc-int/symbol.bi file.
This will save me a day to test these last changes.
Post Reply