2010-08-04 26 views
4

我在d以下代碼關聯數組卸下襬臂在DMD []調用core.stdc.stdio.remove 2.0

import std.stdio; 

class Thing 
{ 
    // Fields 
    private string Name; 
    // Accessors 
    public string name() { return Name; } 
} 

class Place: Thing 
{ 
    // Fields 
    private Place[string] Attached; 
    // Modifiers 
    public void attach(Place place) { Attached[place.name()] = place; } 
    public void detach(Place place) { Attached.remove[place.name()]; } // error line 
} 

每當我試着用dmd2.0編譯我收到以下錯誤

Error: function core.stdc.stdio.remove (in const(char*) filename) is not callable using argument types (Place[string]) 
Error: cannot implicitly convert expression (this.Attached) of type Place[string] to const(char*) 
Error: remove((__error)) must be an array or pointer type, not int 

當前的D2.0文檔建議使用array.remove [key]做我想做的事情,但它看起來像編譯器認爲我試圖調用std.c(stdc?)功能。爲什麼會發生這種情況?這只是dmd2.0中的一個錯誤嗎?

回答

4

使用

Attached.remove(place.name()); 

注意使用括號而不是方括號的嘗試。方括號僅用於索引。

+0

哦,當然是神。 – Eli 2010-08-04 21:02:05

+0

這是一個非常糟糕的錯誤信息...... :( – BCS 2010-08-05 14:32:36

+1

是的,它是。錯誤的原因是因爲D允許你使用'a.fun'調用'fun(a)'類型的函數,所以它開始通過推斷你試圖調用'core.c.stdio.remove',然後嘗試從那裏工作 – 2010-08-06 16:04:00