D编程语言教程-圆形区域-错误:未定义标识符toFloat
我刚开始学习D。当我构建此教程文件时: (直接从D站点),我得到以下错误:D编程语言教程-圆形区域-错误:未定义标识符toFloat,d,D,我刚开始学习D。当我构建此教程文件时: (直接从D站点),我得到以下错误: circle.d|24|Error: template std.stdio.readln cannot deduce function from argument types !()(File), candidates are:| /usr/share/dmd/src/phobos/std/stdio.d|2818| std.stdio.readln(S = string)(dchar terminator
circle.d|24|Error: template std.stdio.readln cannot deduce function from argument types !()(File), candidates are:|
/usr/share/dmd/src/phobos/std/stdio.d|2818| std.stdio.readln(S = string)(dchar terminator = '\x0a') if (isSomeString!S)|
/usr/share/dmd/src/phobos/std/stdio.d|2851| std.stdio.readln(C)(ref C[] buf, dchar terminator = '\x0a') if (isSomeChar!C && is(Unqual!C == C) && !is(C == enum))|
/usr/share/dmd/src/phobos/std/stdio.d|2858| std.stdio.readln(C, R)(ref C[] buf, R terminator) if (isSomeChar!C && is(Unqual!C == C) && !is(C == enum) && isBidirectionalRange!R && is(typeof(terminator.front == (dchar).init)))|
circle.d|25|Error: undefined identifier toFloat|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
你知道为什么吗
提前谢谢
编辑:
因此,我最终得出以下结论:
import std.conv;
import std.stdio;
import std.string;
import std.math;
const real pi = std.math.PI;
void main()
{
try
{
const char [] chRadius;
float r;
writef("Enter the radius: ");
chRadius = chomp(readln());
r = to!float(chRadius);
writefln("Circle area = %f", pi*r*r);
}
catch (Exception e)
{
writefln("catch %s", e.toString());
}
}
生成消息如下所示:
||=== Build: Debug in Area of a Circle (compiler: Digital Mars D Compiler) ===|
circle.d|24|Error: cannot modify const expression chRadius|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
编辑2:将
const char[]
更改为string
,一切正常!谢谢 正如@miken32所提到的,该教程已经过时了——对于现代d编译器,所给出的源代码需要修改:
readln
,不带参数从stdin读取。您可以使用File.readln
读取文件句柄,但在本例中,只需使用chRadius=chomp(readln())
string
而不是char[]
(尽管也可以使用const(char)[]
)来!浮动
而不是toFloat
。!float
表示类型float
是泛型函数to
正如@miken32所提到的,该教程已经过时了——对于现代d编译器,需要修改给出的源代码:
readln
,不带参数从stdin读取。您可以使用File.readln
读取文件句柄,但在本例中,只需使用chRadius=chomp(readln())
string
而不是char[]
(尽管也可以使用const(char)[]
)来!浮动
而不是toFloat
。!float
表示类型float
是泛型函数to
你有一些编译错误,因为这首芭蕾舞。基于D1(旧的、未维护的、过时的语言版本)而非D2(积极开发和广泛使用的语言版本)
如果你想编译这个,那么安装。你有一些编译错误,因为这个图图。基于D1(旧的、未维护的、过时的语言版本)而非D2(积极开发和广泛使用的语言版本)
如果您想编译此文件,请安装。我以前从未看过d,但在我看来,您的问题是readln没有得到任何它能找到的参数。也许自6年前编写教程以来,这些库已经更新了?只值我2美分。检查以下行:
//写于T.McKeaveney,2008年4月代码>。自2008年以来,D发生了很大的变化。我以前从未看过D,但在我看来,你的问题是readln没有得到任何它能找到的参数。也许自6年前编写教程以来,这些库已经更新了?只值我2美分。检查以下行://写于T.McKeaveney,2008年4月代码>。自2008年以来,D发生了很大变化。非常感谢!起初我将char[]
更改为constchar[]
,编译器仍然不喜欢它。不过,一切都与string
一起工作!对不起,应该是const(char)[]
。我已经在上面修复了它,但通常使用字符串(相当于不可变(char)[
)更好。有关更多信息,请参阅。非常感谢!我将char[]
更改为常量char[]
一开始,编译器仍然不喜欢它。不过,一切都是使用字符串的!很抱歉,这应该是const(char)[]
。我已经在上面修复了它,但通常使用字符串(相当于不可变(char)[]
)会更好。有关更多信息,请参阅。