Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a class from a different source file in D?

Tags:

d

I am new to the D language. I am attempting to import my custom class for use in the main() function.

Project struture:

DlangApp/app.d
DlangApp/ClassOne.d

ClassOne.d:

import std.stdio;

class ClassOne
{
    string firstName;
    string lastName;

    this(string first, string last)
    {
        firstName = first;
        lastName = last;
    }

    void writeName()
    {
        writefln("The name is: %s %s", firstName, lastName);
    }
}

app.d:

import std.stdio;
import ClassOne;

void main()
{
    auto aNumber = 10;
    auto aString = "This is a string.";
    writefln("A string: %s\nA number: %s", aString, aNumber);
}

When I run dmd -run app.d, I get this error message:

app.obj(app)
 Error 42: Symbol Undefined _D8ClassOne12__ModuleInfoZ
---errorlevel 1

What am I doing wrong here?

like image 867
quakkels Avatar asked Feb 16 '23 00:02

quakkels


1 Answers

Execute dmd -ofquakkels_app app.d ClassOne.d and, if the compilation was successfull, you will get the quakkels_app executable.

Or, if you really want to use the -run <file> [args...] parameter: dmd ClassOne.d -run app.d . Note that I put -run at the end - because after -run filename you may want to put some parameters that you want to pass to your application.

Now you probably understand why you got the compilation error above - simply DMD did not compile ClassOne.d file...

like image 156
DejanLekic Avatar answered Feb 24 '23 02:02

DejanLekic