我在 Ubuntu 13.04 64 位上使用 DMD64 D Compiler v2.063.2。
我写了一个类如下:
class FixedList(T){
// list
private T[] list;
// number of items
private size_t numberOfItems;
// capacity
private size_t capacity;
// mutex
private Mutex listMutex;
// get capacity
@property public size_t Capacity(){ return capacity; }
@property public shared size_t Capacity(){ return capacity; }
// constructor
public this( size_t capacity ){
// initialise
numberOfItems = 0;
this.capacity = capacity;
writeln("Cons Normal");
}
// constructor
public shared this( size_t capacity ){
// initialise
numberOfItems = 0;
this.capacity = capacity;
// create mutex
listMutex = cast(shared)(new Mutex());
writeln("Cons Shared");
}
}
虽然以这种方式编写类,但在 main 函数中,我编写了该代码:
auto list1 = new shared FixedList!int( 128 );
auto list2 = new FixedList!int( 128 );
用这个输出,完全没有错误,输出如下:
Cons Shared
Cons Normal
我接下来要做的是writeln
从代码中删除这两行,当我重新编译代码时,它开始显示如下错误消息:
src/webapp.d(61): Error: constructor lists.FixedList!(int).FixedList.this called with argument types:
((int) shared)
matches both:
lists.d(28): lists.FixedList!(int).FixedList.this(ulong capacity)
and:
lists.d(37): lists.FixedList!(int).FixedList.this(ulong capacity)
src/app.d(61): Error: no constructor for FixedList
src/app.d(62): Error: constructor lists.FixedList!(int).FixedList.this called with argument types:
((int))
matches both:
lists.d(28): lists.FixedList!(int).FixedList.this(ulong capacity)
and:
lists.d(37): lists.FixedList!(int).FixedList.this(ulong capacity)
src/app.d(62): Error: no constructor for FixedList
make: *** [all] Error 1
基本上writeln
功能是防止错误。实际上writeln
在很多地方都在预防,我不确定为什么会这样。
我什至尝试用m32
32 位标志编译代码,但它仍然相同。我做错了什么,还是这是一个错误?