您好我用dlang语言写了以下代码:nogc nothrowexternC__gsharedimport corestdcstdio;struct People string name=not inited; int age=0; ~this printfnamesaged is freenameptrage; People createPeoples
您好,根据编译器的报错,可以看出问题出在析构函数上。您可以将析构函数标记为@nogc nothrow,如下所示:
@nogc nothrow: extern(C): __gshared: import core.stdc.stdio; struct People { string name="not inited"; int age=0; @nogc nothrow: ~this() { printf("name:%s,age:%d is free",name.ptr,age); } } People createPeople(string name="chenfa",int age=20) { People p = People(name,age); return p; } @nogc nothrow: int main() { createPeople(); return 0; }
此外,由于main函数也被标记为@nogc nothrow,因此在createPeople函数中,您需要使用nothrow关键字来确保不会抛出异常,如下所示:
People createPeople(string name="chenfa",int age=20) nothrow { People p = People(name,age); return p; }
希望这能帮助您解决问题
原文地址: https://www.cveoy.top/t/topic/fInb 著作权归作者所有。请勿转载和采集!