0
votes

I've recently began learning D, and furthermore templates. I wanted to make a small example to reinforce the reading, but then I get this error above.

Specifically for my code, it says:

using_point.obj(using_point)
Error 42: Symbol Undefined _D6points7_arrayZ --- errorlevel1

Here's my code:

module point;

class Point(Type: long) {

     public string name;
     public Type[] locations;

     alias Type T;

     this(string name, Type[] magnitudes) {
         this.name = name;

         for(int i = 0; i < magnitudes.length; i++)
              locations ~= magnitudes[i];
     }  

     override
     public string toString() {
         string output = this.name ~ " = (" ~ this.locations[0];

         for(int i = 1; i < locations.length; i++)
             output ~= "," ~ this.locations[i];

         output ~= ")";

         return output;     
     }
}

and main:

module using_point;
import std.stdio;
import point;


void main() {
    byte[] mags = [1,2,3];
    auto p1 = new Point!byte("P", mags);
}

I understand that it's a linking error, but since I'm not using external libraries and I think I've properly defined the constructor for Point, I can't seem to find the problem.

1
What's your compile command? If you sue dmd main.d point.d - both files together at once - it should work... odds are you did them separately though - Adam D. Ruppe
This is not an answer but you are missing some .to!string calls in your toString method. Everywhere where you try to append this.locations[N] to a string, you need to change it to this.locations[N].to!string if you want it to work with Type or T being anything else than string, char or (u)byte - WebFreak001
My commands were "dmd -c point.d," then "dmd main.d." Huh, odd. I assumed that wouldn't be necessary because of the import statement. And, thanks WebFreak I'll make sure to do that. I actually planned on it, just spaced it out. Do you know the import statement needed? - Anthony Monterrosa
You're correct, it compiled successfully with "dmd point.d using_point.d," but I'm still confused by the source of the original error, which is still thrown with "dmd using_point.d." - Anthony Monterrosa

1 Answers

2
votes

From the comments, you appear to be running the following:

dmd -c point.d
dmd main.d # presumably contains `import point;`

The first command compiles (but does not link) point.d into point.o.

The second command compiles and links main.d, and only main.d, into an executable. It fails with a "missing symbol" error because DMD will not look for or generate code for anything not on its command line. When you import points;, you only import the symbols, not the actual code.

To fix this, either...

  1. Compile points.d together with main.d:

    dmd main.d points.d
    
  2. Link with points.o:

    dmd -c points.d
    dmd main.d points.o
    
  3. Or use rdmd, which will scan all your imports figure out what needs to be compiled, compile the program, and run it:

    rdmd main.d