Author Topic: Errors in an $include file  (Read 3560 times)

0 Members and 1 Guest are viewing this topic.

Offline justsomeguy

  • Newbie
  • Posts: 47
    • View Profile
Errors in an $include file
« on: May 16, 2021, 08:25:51 pm »
Hello

I'm getting an error in files that I've included into my main program. I know the cause of the errors, but they are not really errors.

For example my main program will look like this.
Code: QB64: [Select]
  1. '**********
  2. ' Main Program
  3. '**********
  4. Type tTEST
  5.     a As Integer
  6.  
  7. Dim test As tTEST
  8.  
  9. test.a = 5
  10. Print test.a, aPlus(test)
  11.  
  12. '$include :'TestModule.bas'

And my module will look like this.

Code: QB64: [Select]
  1. '**********
  2. ' TestModule.bas
  3. '**********
  4. FUNCTION aPlus (t AS tTEST)
  5.     aPlus = t.a + 1

Don't get me wrong, the main program works and it compiles with no errors.

The problem arises when I want to edit my module. I get an 'Illegal SUB/FUNCTION parameter.' This makes sense since because it has no idea what a 'tTEST' in this context.

Have I missed something? Is there some clever workaround for this?

FellippeHeitor

  • Guest
Re: Errors in an $include file
« Reply #1 on: May 16, 2021, 08:37:39 pm »
The error is exactly what you pointed out - the TYPE is not visible to the sub file, except when it's included. TYPEs can be placed anywhere in your program though (even between subs/functions):

Code: QB64: [Select]
  1. '**********
  2. ' Main Program
  3. '**********
  4. Dim test As tTEST
  5.  
  6. test.a = 5
  7. Print test.a, aPlus(test)
  8.  
  9. '$include :'TestModule.bas'

Code: QB64: [Select]
  1. '**********
  2. ' TestModule.bas
  3. '**********
  4. Type tTEST
  5.     a As Integer
  6.  
  7. Function aPlus (t As tTEST)
  8.     aPlus = t.a + 1
  9.  
« Last Edit: May 16, 2021, 08:39:39 pm by FellippeHeitor »

Offline justsomeguy

  • Newbie
  • Posts: 47
    • View Profile
Re: Errors in an $include file
« Reply #2 on: May 16, 2021, 09:03:58 pm »
Quote
TYPEs can be placed anywhere in your program though (even between subs/functions)

@FellippeHeitor  Well to expand on my example. I'm trying to refactor the physics engine I'm working on, and I literally use some TYPEs like 'tVECTOR2d' all over the place. I have numerous modules using 'tVECTOR2d'. So, declaring the type in the module wont clear the error in the other modules.

I was just wondering if my implementation is incorrect, or some command to tell the module that it is a module and suppress that error?

FellippeHeitor

  • Guest
Re: Errors in an $include file
« Reply #3 on: May 16, 2021, 09:06:36 pm »
No other way, really. And nothing wrong with your implementation, it's just the way it is.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Errors in an $include file
« Reply #4 on: May 17, 2021, 11:17:56 am »
@FellippeHeitor  Well to expand on my example. I'm trying to refactor the physics engine I'm working on, and I literally use some TYPEs like 'tVECTOR2d' all over the place. I have numerous modules using 'tVECTOR2d'. So, declaring the type in the module wont clear the error in the other modules.

I was just wondering if my implementation is incorrect, or some command to tell the module that it is a module and suppress that error?

Could put the Type in a separate .BI file  and include at start of main
Put the vector subs in a .BM and include at end of main, note all these will show error alone but once connected to Bi in main will work.

Code: QB64: [Select]
  1. 'ttest.bi
  2. Type tTEST
  3.     a As Integer
  4.  

Code: QB64: [Select]
  1. ' Main test.bas
  2.  
  3. '$include :'ttest.bi'
  4. Dim test As tTEST
  5.  
  6. test.a = 5
  7. Print test.a, aPlus(test)
  8.  
  9. '$include :'aplus.bm'
  10.  

Code: QB64: [Select]
  1. 'aplus.bm
  2. Function aPlus (t As tTEST)
  3.     aPlus = t.a + 1
  4.  

Here is zip for your convenience to see
« Last Edit: May 17, 2021, 11:20:19 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Errors in an $include file
« Reply #5 on: May 17, 2021, 11:48:23 am »
@justsomeguy Oh you want to edit the .BM how about just adding the .BI in include and commenting it out after edit (ie add / remove $)

Code: QB64: [Select]
  1. 'aplus.bm
  2. 'include: 'ttest.bi'  '<<< add $ to edit, remove to save
  3. Function aPlus (t As tTEST)
  4.     aPlus = t.a + 1
  5.  
« Last Edit: May 17, 2021, 11:52:53 am by bplus »

Offline thesolarcode

  • Newbie
  • Posts: 8
    • View Profile
Re: Errors in an $include file
« Reply #6 on: May 17, 2021, 02:02:51 pm »
Is there a smart way to conditionally include some file only once?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Errors in an $include file
« Reply #7 on: May 17, 2021, 02:26:45 pm »
Easiest way is with $IF


$IF VARIABLENAMEYOULIKE = UNDEFINED THEN
    VARIABLENAMEYOULIKE = TRUE
    SUB MyLibrarySub
    .
    .
    .
    END SUB
$END IF

Include that in a dozen $INCLUDE files and it’ll only go into your code one (while the precompiler variable is undeclared), and then it’ll be skipped over forevermore.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline justsomeguy

  • Newbie
  • Posts: 47
    • View Profile
Re: Errors in an $include file
« Reply #8 on: May 17, 2021, 07:39:00 pm »
Thank you gentlemen! You have given me some options. This will go a long way to clean up the code.

Offline justsomeguy

  • Newbie
  • Posts: 47
    • View Profile
Re: Errors in an $include file
« Reply #9 on: May 17, 2021, 09:18:36 pm »
@SMcNeill You method seems to work except minor thing. I had to use the $LET.

I had to change it to:
Code: QB64: [Select]
  1. $IF VARIABLENAMEYOULIKE = UNDEFINED THEN
  2.     $LET VARIABLENAMEYOULIKE = TRUE
  3.     SUB MyLibrarySub
  4.        .
  5.        .
  6.        .

This works good. THANKS!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Errors in an $include file
« Reply #10 on: May 18, 2021, 12:36:01 am »
Aye.  Sorry.  I forgot to add the $LET in there.  :)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!