Recent

Author Topic: [Solved] Inherited child class using separate files  (Read 3719 times)

nikel

  • Full Member
  • ***
  • Posts: 186
[Solved] Inherited child class using separate files
« on: October 19, 2021, 08:46:17 pm »
Hello, I'm trying to make main class as parent and inherit its child class, so it could overload parent functions.

Code: Pascal  [Select][+][-]
  1. Unit AnalyzeParameters;
  2.  
  3. {$Mode Objfpc}{$H+}
  4.  
  5. Interface
  6.  
  7. Uses
  8.   Classes, SysUtils,
  9.   Application, Token, Config;
  10.  
  11. Type
  12.   PAnalyzeParameters = ^TAnalyzeParameters;
  13.  
  14.   { TAnalyzeParameters }
  15.  
  16.   TAnalyzeParameters = Class(TMain);
  17.   Private
  18.  
  19.   Protected
  20.     Procedure DoRun;
  21.   Public
  22.     Constructor Create(TheOwner: TComponent);
  23.     Destructor Destroy;
  24.   End;
  25.  
  26. Implementation

Code: Pascal  [Select][+][-]
  1. Program Project1(Input, Output, StdErr);
  2.  
  3. {$Mode Objfpc}{$H+}
  4. {$Typedaddress On}
  5.  
  6. Uses
  7.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  8.   CThreads, Unix,
  9.   {$EndIF}{$EndIF}
  10.   Classes, SysUtils,
  11.   { you can add units after this }
  12.   {$IFDEF WINDOWS}
  13.   Windows, Dos, LCLIntF, MMSystem, LazUTF8,
  14.   {$EndIF}
  15.   FileUtil, Process, StrUtils, LazLoggerBase, DateUtils, Math, CustApp,
  16.   Character,
  17.   VInfo, VersionTypes,
  18.   Config, AnalyzeParameters, Token;
  19.  
  20.   Type
  21.  
  22.   { TMain }
  23.  
  24.   TMain = class(TCustomApplication)
  25.   Protected
  26.     Procedure DoRun; Override;
  27.   Public
  28.   ...
  29. Var
  30.   Application: TMain;
  31.  
  32. {$R *.res}
  33.  
  34. Begin
  35.   Application:=TMain.Create (Nil);
  36.   Application.Title:='Playlist Program';
  37.   Application.Run;
  38.   Application.Free;
  39. End.

This is giving me error:
Quote
analyzeparameters.pas(9,3) Fatal: Cannot find Application used by AnalyzeParameters of the Project Inspector.

How can create a type at separate file?
 
« Last Edit: October 23, 2021, 01:04:42 am by nikel »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Inherited child class using separate files
« Reply #1 on: October 19, 2021, 08:56:28 pm »
You've not shown us enough of your code to be useful (FORUM RULE: ALWAYS POST SOMETHING WHICH ANYBODY TRYING TO HELP YOU CAN TRY TO COMPILE) but my guess would be that you simply need to import Forms into your implementation part.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Inherited child class using separate files
« Reply #2 on: October 19, 2021, 08:57:25 pm »
Do you actually have a unit called Application?
If so, that is a no-no in an LCL app, where Application is a global variable declared in the Forms unit.
You cannot reuse the same identifier as a unit name in your uses clause.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Inherited child class using separate files
« Reply #3 on: October 19, 2021, 09:01:30 pm »
Well spotted Howard :-)

I'm afraid I gave up with the quoted code when I tried scrolling down to find what had caused the error message...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Inherited child class using separate files
« Reply #4 on: October 19, 2021, 09:46:55 pm »
Thanks for the replies. I modified my topic and added a part of Application class.

Regarding to your replies I tried adding "Project1" to uses section but it didn't work either. I mean I tried adding these three words to uses section: Application, TMain and Project1.
« Last Edit: October 19, 2021, 09:50:05 pm by nikel »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Inherited child class using separate files
« Reply #5 on: October 19, 2021, 09:59:45 pm »
Regarding to your replies I tried adding "Project1" to uses section but it didn't work either. I mean I tried adding these three words to uses section: Application, TMain and Project1.

Who suggested that you add Application and TMain?

Howard explicitly told you to not call something Application...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Inherited child class using separate files
« Reply #6 on: October 20, 2021, 02:00:57 am »
get the nice of plastics

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Inherited child class using separate files
« Reply #7 on: October 20, 2021, 09:06:24 am »
This is giving me error:
Quote
analyzeparameters.pas(9,3) Fatal: Cannot find Application used by AnalyzeParameters of the Project Inspector.

How can create a type at separate file?

Independant of the error you get you have a separate problem: you can't inherit from a class declared in the main project file (your TMain) inside a unit (your TAnalyzeParameters). If you want to do it that way you need to move your TMain to a separate unit as well, use that from your AnalyzeParameters unit and then instantiate your Application variable in your main project file using TAnalyzeParameters.Create instead of TMain.Create. If you do it like that then you don't need to add Application to your uses-clause in AnalyzeParameters (which was wrong anyway).

Additional side note: you don't need to declare a pointer type for your TAnalyzeParameters. It's really, really seldom that you need a pointer type for a class, because classes are hidden pointers by themselves already.

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Inherited child class using separate files
« Reply #8 on: October 21, 2021, 06:33:19 pm »
Thanks for the detailed reply.

Quote
need to move your TMain to a separate unit as well

I understand from above I must copy TMain content to the new file for reference. Am I right?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Inherited child class using separate files
« Reply #9 on: October 22, 2021, 03:55:38 pm »
I understand from above I must copy TMain content to the new file for reference. Am I right?

You must move it there to work correctly. The main project must not contain the declaration of TMain.

 

TinyPortal © 2005-2018