我碰到,我是无法尝试使用模板的静态函数addID
以下时,要解决一个错误:Dlang静态模板结构/类函数
module static_example;
struct Foo(T: long) {
public static int addID(E)(E foo1, E foo2) {
return foo1.get() + foo2.get();
}
private:
T ID;
alias T Type;
public this(T ID) {
this.ID = ID;
}
public T get() {
return this.ID;
}
}
我收到电话rdmd static_example.d
错误是:
Error: template Foo(T : long) does not have property 'addID'
我真的不明白这个错误。我认为这与字面标记@property
(似乎不是,但我不明白@property
函数)没有问题,所以我做了几个其他结构主要一起测试所有:
struct Bar {
public static int addID(Bar bar1, Bar bar2) {
return bar1.get() + bar2.get();
}
private:
int ID;
public this(int ID) {
this.ID = ID;
}
public int get() {
return this.ID;
}
}
struct Batz(T: long){
public int addID(E)(E foo) {
return this.get() + foo.get();
}
private:
T ID;
alias T Type;
public this(T ID) {
this.ID = ID;
}
public T get() {
return this.ID;
}
}
void main() {
auto foo1 = Foo!int(27);
auto foo2 = Foo!int(13);
int staticTemplateAdd = Foo.addID(foo1, foo2);
auto bar1 = Bar(27);
auto bar2 = Bar(13);
int staticAdd = Bar.addID(bar1, bar2);
auto batz1 = Batz!int(27);
auto batz2 = Batz!int(13);
int templateAdd = batz1.addID(batz2);
}
这些其他类实际上是相同的,不同之处Bar
使用静态addID
和Batz
使用模板addID
而foo是静态和模板。
Foo
是引发错误的唯一结构,并且我很难过。我正在关注一个D模板教程(如果您有兴趣,可以在顶部附近找到https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.md),但是它的例子对我来说都是外来的,并且似乎与我正在尝试做的有些不同。任何人都知道这里发生了什么?
你打算怎么调用'addID'?请扩展您的问题。 – sigod
@sigod main位于代码的底部。对不起,我可以让它更明显。 –