www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use classes from another d files

reply Vinod K Chandran <kcvinu82 gmail.com> writes:
Hi all,
I am new to D. But some fair experience with vb.net. I was 
playing with D classes.  I wrote a class in a D file.
The file name is "classFile.d"
```D
class TestClass {

     int myIntVar        ;
     string myStringVar  ;

     this(int miv, string msv) {
         this.myIntVar = miv     ;
         this.myStringVar = msv  ;
     }//End this

}//End Class
```
And i wrote my main function in another file named "caller.d". 
Both these files are in same folder.
```D
import std.stdio ;

void main() {
     import classFile ;


     TestClass tc = new TestClass(37, "Sample String") ;
     writeln(tc.myStringVar) ;
     tc.myIntVar = 45 ;
     writeln(tc.myIntVar)  ;

     writeln("its over..") ;

}//End main
```
This is the error message i got.
;------------------------------------------------
caller.obj(caller)
  Error 42: Symbol Undefined 
__D9classFile9TestClass6__ctorMFiAyaZCQBjQBc
caller.obj(caller)
  Error 42: Symbol Undefined __D9classFile9TestClass7__ClassZ
Error: linker exited with status 2
PS E:\OneDrive Folder\OneDrive\Programming\D 
Programming\Samples\Sample2>
Oct 22 2019
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 22 October 2019 at 17:34:51 UTC, Vinod K Chandran 
wrote:
 Hi all,
 I am new to D. But some fair experience with vb.net. I was 
 playing with D classes.  I wrote a class in a D file.
 The file name is "classFile.d"
did you compile with dmd -i or list both files on the dmd command line?
Oct 22 2019
parent reply Vinod K Chandran <kcvinu82 gmail.com> writes:
On Tuesday, 22 October 2019 at 17:38:58 UTC, Adam D. Ruppe wrote:
 On Tuesday, 22 October 2019 at 17:34:51 UTC, Vinod K Chandran 
 wrote:
 Hi all,
 I am new to D. But some fair experience with vb.net. I was 
 playing with D classes.  I wrote a class in a D file.
 The file name is "classFile.d"
did you compile with dmd -i or list both files on the dmd command line?
Thanks for the reply. Nope. I just typed this code in VS Code and hit the run button. Thats all. i think this cmd business will be difficult for me.
Oct 22 2019
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 10/22/2019 11:25 AM, Vinod K Chandran wrote:
 On Tuesday, 22 October 2019 at 17:38:58 UTC, Adam D. Ruppe wrote:
 On Tuesday, 22 October 2019 at 17:34:51 UTC, Vinod K Chandran wrote:
 Hi all,
 I am new to D. But some fair experience with vb.net. I was playing
 with D classes.  I wrote a class in a D file.
 The file name is "classFile.d"
did you compile with dmd -i or list both files on the dmd command line?
That: The -i switch is the answer to your question in the other post ("what if there is too many include files").
 Thanks for the reply. Nope. I just typed this code in VS Code and hit
 the run button. Thats all.
But not sufficient. :) IDEs like VS Code have a way of describing what your project involves. You need to add the two files to you "project". (I don't know the details for your IDE.)
 i think this cmd business will be difficult
 for me.
Yes, IDEs make it convenient. Ali
Oct 22 2019
parent Vinod K Chandran <kcvinu82 gmail.com> writes:
On Tuesday, 22 October 2019 at 18:33:32 UTC, Ali Çehreli wrote:
 On 10/22/2019 11:25 AM, Vinod K Chandran wrote:
 On Tuesday, 22 October 2019 at 17:38:58 UTC, Adam D. Ruppe
wrote:
 [...]
Chandran wrote:
 [...]
playing
 [...]
command line? That: The -i switch is the answer to your question in the other post ("what if there is too many include files").
 Thanks for the reply. Nope. I just typed this code in VS Code
and hit
 the run button. Thats all.
But not sufficient. :) IDEs like VS Code have a way of describing what your project involves. You need to add the two files to you "project". (I don't know the details for your IDE.)
 i think this cmd business will be difficult
 for me.
Yes, IDEs make it convenient. Ali
Ali Çehreli, Thanks for the detailed reply. I have tested with "dmd -i" and it worked.
Oct 24 2019
prev sibling parent reply Daniel Kozak <kozzi11 gmail.com> writes:
On Tue, Oct 22, 2019 at 8:30 PM Vinod K Chandran via
Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 On Tuesday, 22 October 2019 at 17:38:58 UTC, Adam D. Ruppe wrote:
 On Tuesday, 22 October 2019 at 17:34:51 UTC, Vinod K Chandran
 wrote:
 Hi all,
 I am new to D. But some fair experience with vb.net. I was
 playing with D classes.  I wrote a class in a D file.
 The file name is "classFile.d"
did you compile with dmd -i or list both files on the dmd command line?
Thanks for the reply. Nope. I just typed this code in VS Code and hit the run button. Thats all. i think this cmd business will be difficult for me.
You should use dub. You will probably need it anyway in the future. VS Code have nice plugins to work with D you can install D Language utility extension pack
Oct 22 2019
parent reply Vinod K Chandran <kcvinu82 gmail.com> writes:
On Tuesday, 22 October 2019 at 18:34:52 UTC, Daniel Kozak wrote:
 On Tue, Oct 22, 2019 at 8:30 PM Vinod K Chandran via 
 Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 On Tuesday, 22 October 2019 at 17:38:58 UTC, Adam D. Ruppe 
 wrote:
 On Tuesday, 22 October 2019 at 17:34:51 UTC, Vinod K 
 Chandran wrote:
 [...]
did you compile with dmd -i or list both files on the dmd command line?
Thanks for the reply. Nope. I just typed this code in VS Code and hit the run button. Thats all. i think this cmd business will be difficult for me.
You should use dub. You will probably need it anyway in the future. VS Code have nice plugins to work with D you can install D Language utility extension pack
Daniel Kozak, Thanks. Let me try it. :)
Oct 24 2019
parent Vinod K Chandran <kcvinu82 gmail.com> writes:
On Thursday, 24 October 2019 at 10:19:23 UTC, Vinod K Chandran 
wrote:
 On Tuesday, 22 October 2019 at 18:34:52 UTC, Daniel Kozak wrote:
 On Tue, Oct 22, 2019 at 8:30 PM Vinod K Chandran via 
 Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 On Tuesday, 22 October 2019 at 17:38:58 UTC, Adam D. Ruppe 
 wrote:
 [...]
Thanks for the reply. Nope. I just typed this code in VS Code and hit the run button. Thats all. i think this cmd business will be difficult for me.
You should use dub. You will probably need it anyway in the future. VS Code have nice plugins to work with D you can install D Language utility extension pack
Daniel Kozak, Thanks. Let me try it. :)
Is there any tutorials related to using dub in d lang ?
Oct 24 2019
prev sibling parent reply Arun Chandrasekaran <aruncxy gmail.com> writes:
On Tuesday, 22 October 2019 at 17:34:51 UTC, Vinod K Chandran 
wrote:
 Hi all,
 I am new to D. But some fair experience with vb.net. I was 
 playing with D classes.  I wrote a class in a D file.
 The file name is "classFile.d"
 ```D
 class TestClass {

 [...]
What you are seeing is a linker error. Build it as follows: dmd caller.d classFile.d Unrelated, first line of classFile.d needs be `module classFile`
Oct 22 2019
parent reply Vinod K Chandran <kcvinu82 gmail.com> writes:
On Tuesday, 22 October 2019 at 17:40:11 UTC, Arun Chandrasekaran 
wrote:
 On Tuesday, 22 October 2019 at 17:34:51 UTC, Vinod K Chandran 
 wrote:
 Hi all,
 I am new to D. But some fair experience with vb.net. I was 
 playing with D classes.  I wrote a class in a D file.
 The file name is "classFile.d"
 ```D
 class TestClass {

 [...]
What you are seeing is a linker error. Build it as follows: dmd caller.d classFile.d Unrelated, first line of classFile.d needs be `module classFile`
Thanks a lot. But what if there is too many include files ? Say, i have 10 files to import in main file, Do i need to type all the file names in cmd ? Is there any dedicated ide in order to avoid this typing task ? I have tested it with Codeblocks, but didn't worked.
Oct 22 2019
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 22 October 2019 at 18:21:36 UTC, Vinod K Chandran 
wrote:
 But what if there is too many include files ?
The dmd -i thing will do that for you dmd -i main.d and it will automatically find the others, assuming they are laid out so the module/import name matches the file name
Oct 22 2019
parent Vinod K Chandran <kcvinu82 gmail.com> writes:
On Tuesday, 22 October 2019 at 18:31:17 UTC, Adam D. Ruppe wrote:
 On Tuesday, 22 October 2019 at 18:21:36 UTC, Vinod K Chandran 
 wrote:
 But what if there is too many include files ?
The dmd -i thing will do that for you dmd -i main.d and it will automatically find the others, assuming they are laid out so the module/import name matches the file name
Adam D. Ruppe, Thanks. It worked. :)
Oct 24 2019