Dlang 错误 42:符号未定义

Dlang Error 42: Symbol Undefined

我最近开始学习D,还有模板。我想做一个小例子来加强阅读,但后来我得到了上面的这个错误。

专门针对我的代码,它说:

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

这是我的代码:

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;     
     }
}

和主要:

module using_point;
import std.stdio;
import point;


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

我知道这是一个链接错误,但由于我没有使用外部库,而且我认为我已经正确定义了 Point 的构造函数,所以我似乎找不到问题所在。

从评论来看,您似乎运行宁以下:

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

第一个命令编译(但不link)point.dpoint.o

第二条命令将links main.dmain.d编译成可执行文件。它因 "missing symbol" 错误而失败,因为 DMD 不会为不在其命令行上的任何内容查找或生成代码。当您 import points; 时,您只导入符号,而不是实际代码。

要解决此问题,要么...

  1. 编译points.dmain.d:

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

    dmd -c points.d
    dmd main.d points.o
    
  3. 或者使用rdmd,它会扫描你所有的导入找出需要编译的东西,编译程序,然后运行它:

    rdmd main.d