声明 "MyStuct a();" 和 "MyStruct a;" 有什么区别?
What is the difference between declaration "MyStuct a();" and "MyStruct a;"?
该程序编译链接:
struct A{
int m;
}
void main()
{
A a;
int x = a.m;
}
虽然这不是:
struct A{
int m;
}
void main()
{
A a();
int x = a.m;
}
$dmd app.d
returns:
app.o: In function `_Dmain':
app.d:(.text._Dmain+0xb): undefined reference to `_D3app4mainFZ1aMFZS3app1A'
collect2: error: ld returned 1 exit status
我很纳闷
A a();
是一个函数声明 - 它声明一个名为 a
的函数,其中 returns 结构类型的值 A
.int x = a.m;
然后调用函数 a
(如果不需要参数,括号在 D 中是可选的),并读取返回值的 m
字段。
因为函数只被声明,但从未定义,程序编译但不link,因为函数a
没有linker的主体可以看到。
该程序编译链接:
struct A{
int m;
}
void main()
{
A a;
int x = a.m;
}
虽然这不是:
struct A{
int m;
}
void main()
{
A a();
int x = a.m;
}
$dmd app.d
returns:
app.o: In function `_Dmain':
app.d:(.text._Dmain+0xb): undefined reference to `_D3app4mainFZ1aMFZS3app1A'
collect2: error: ld returned 1 exit status
我很纳闷
A a();
是一个函数声明 - 它声明一个名为 a
的函数,其中 returns 结构类型的值 A
.int x = a.m;
然后调用函数 a
(如果不需要参数,括号在 D 中是可选的),并读取返回值的 m
字段。
因为函数只被声明,但从未定义,程序编译但不link,因为函数a
没有linker的主体可以看到。