[SOLVED] Using an Existing Function Inside a TYPE Class

General FreeBASIC programming questions.
Post Reply
exagonx
Posts: 339
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

[SOLVED] Using an Existing Function Inside a TYPE Class

Post by exagonx »

Hi friends, after some time I found myself having to create something more complex and having the need to use functions within a class, now the function is inside a library shared between multiple executables since it is very large including it in each executable is quite expensive especially in the update where I have to update multiple executables having to recompile them from scratch.

This is the code:

Code: Select all

' Declaration of the function inside the LIB
Declare Function CLDESIGN_PRINTAREA(ByVal DoubleRow As Integer = 0, ByVal SquareExtend As String = ""))As Integer

'This is little code that replace the big function
Function CLDESIGN_PRINTAREA( ByVal DoubleRow As Integer = 0, ByVal SquareExtend As String = "")As Integer export
	Dim As Integer Status = 0, Extension = 0
	
	If Len(SquareExtend) > 0 Then
		Extension = 1
		Status = 0
	Else 
		Status = 1
	End If
	
	
	
	Return Status
End Function

'The class where I Have to use the function
type Square
	private:
		Dim As Integer XResolution = 640, YResolution = 400 ' Default DOS Resolution for command line
		
	public:	
		Static CLDE_PRINTAREA As Function (ByVal DoubleRow As Integer = 0, ByVal SquareExtend As String = ""))As Integer
		 


End Type

' Here I assign the pointer
Square.CLDE_PRINTAREA=@CLDESIGN_PRINTAREA() 'Assign pointer to a function


And when i compile it give me this message

ld.exe: test.o:fake:(.rdata$.refptr._ZN6SQUARE15CLDE_PRINTAREA$E[.refptr._ZN6SQUARE15CLDE_PRINTAREA$E]+0x0): undefined reference to `SQUARE::CLDE_PRINTAREA$'
I apologize if I got confused with something trivial but I've been working on a PHP project for 7 months and getting back into FreeBASIC is proving more difficult than I thought. Among my old projects I can't find anything that's relevant.
Last edited by exagonx on Jan 17, 2025 11:29, edited 1 time in total.
fxm
Moderator
Posts: 12473
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Using an Existing Function Inside a TYPE Class

Post by fxm »

A static member variable of a Type must not only be declared in the Type but also allocated outside the Type (see STATIC (Member)):

Code: Select all

' Declaration of the function inside the LIB
Declare Function CLDESIGN_PRINTAREA(ByVal DoubleRow As Integer = 0, ByVal SquareExtend As String = "") As Integer

'This is little code that replace the big function
Function CLDESIGN_PRINTAREA( ByVal DoubleRow As Integer = 0, ByVal SquareExtend As String = "") As Integer export
	Dim As Integer Status = 0, Extension = 0
	
	If Len(SquareExtend) > 0 Then
		Extension = 1
		Status = 0
	Else 
		Status = 1
	End If
	
	Return Status
End Function

'The class where I Have to use the function
type Square
	private:
		Dim As Integer XResolution = 640, YResolution = 400 ' Default DOS Resolution for command line
		
	public:	
		Static CLDE_PRINTAREA As Function (ByVal DoubleRow As Integer = 0, ByVal SquareExtend As String = "") As Integer
End Type

' Here I allocate the function pointer and assign a pointer value
Dim As Function (ByVal DoubleRow As Integer = 0, ByVal SquareExtend As String = "") As Integer Square.CLDE_PRINTAREA = @CLDESIGN_PRINTAREA() 'Assign pointer value to a function pointer
exagonx
Posts: 339
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Using an Existing Function Inside a TYPE Class

Post by exagonx »

fxm wrote: Jan 17, 2025 10:02 A static member variable of a Type must not only be declared in the Type but also allocated outside the Type (see STATIC (Member)):
Then I have to declare like a normal function inside the type but using STATIC instead of DIM

Code: Select all

Type MyClas
      STATIC MYMETHOD As Function ( Param as Param) As param

End Type
And Outside of Type I Have do dimension assign the pointer

Code: Select all

Dim As Function (Param as Param) as param MYMETHOD = @MYFUNCTION
Thank you Give no Error during compiling I hope I understood
fxm
Moderator
Posts: 12473
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [SOLVED] Using an Existing Function Inside a TYPE Class

Post by fxm »

Dim As Function (Param as Param) as param MyClas.MYMETHOD = @MYFUNCTION
exagonx
Posts: 339
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: [SOLVED] Using an Existing Function Inside a TYPE Class

Post by exagonx »

fxm wrote: Jan 17, 2025 11:52 Dim As Function (Param as Param) as param MyClas.MYMETHOD = @MYFUNCTION
Thanks for the correction, having called Method and not function I assumed that the class name was implicit
fxm
Moderator
Posts: 12473
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [SOLVED] Using an Existing Function Inside a TYPE Class

Post by fxm »

If the typename is not specified, the compiler allocates memory for another non-member variable outside the Type namespace, and thus different from the one declared in the Type.
Post Reply