我无法在以下代码中打印双精度值?

发布于 2024-11-16 01:07:32 字数 6527 浏览 2 评论 0原文

#include <iostream>
#include <iomanip>

using namespace std;

double distance(double, double);

int main ()
{
    double rate, time, distanceValue;

    cout << fixed << showpoint << setprecision(1);

    cout << "Enter rate" << endl;
    cin >> rate;

    cout << "Enter time" << endl;
    cin >> time;

    distanceValue = distance(rate, time);

    cout << "The distance is " << distanceValue << endl;
}

double distance (double num1, double num2)
{
    return num1 * num2;
}

当我尝试编译它时,我看到一长串错误,我不知道这里出了什么问题!另外,如果我将方法从 double 更改为 int 值,它效果很好!为什么??

出现以下错误:

*> C:\用户\ kifcaliph \桌面\启动

用c++\Chapter6>cl 06_012.cpp 输出 06_012 微软 (R) 32 位 C/C++ 优化编译器版本 16.00.30319.01 for 80x86 版权所有 (C) Microsoft Corporation。版权所有 保留。

cl:命令行警告 D9024: 无法识别的源文件类型 '06_012',假定为目标文件 06_012.cpp C:\程序文件 (x86)\微软 Visual Studio 10.0\VC\INCLUDE\xlocale(323) :警告 C4530:使用了 C++ 异常处理程序, 但未启用展开语义。 指定 /EHsc C:\Program Files (x86)\微软 Visual Studio 10.0\VC\INCLUDE\xutility(373) : 错误 C2825: '_Iter': 必须是一个类或 后跟“::”的命名空间 06_012.cpp(20) :参见类模板实例化参考 'std::iterator_traits<_Iter>'存在 编译的 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(373):错误 C2039:“iterator_category”:不是 “全局命名空间”的成员 C:\Program Files (x86)\Microsoft 视觉工作室 10.0\VC\INCLUDE\xutility(373):错误 C2146:语法错误:缺少“;”乙 在标识符“iterator_category”之前 C:\Program Files (x86)\Microsoft 视觉工作室 10.0\VC\INCLUDE\xutility(373) : 错误 C2602: 'std::iterator_traits<_Iter>; ::iterator_category' 不是成员 的基类的 'std::iterator_traits<_Iter>' 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(373) :参见 'std::iterator 的声明 _traits<_Iter>::iterator_category' 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(373) : 错误 C2868: 'std::iterator_traits<_Iter>; ::iterator_category' :非法语法 用于使用声明;预期的 限定名称 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(374) : 错误 C2825: '_Iter': 必须是一个类或 后跟“::”的命名空间 C:\Program Files (x86)\Microsoft 视觉工作室 10.0\VC\INCLUDE\xutility(374):错误 C2039:“value_type”:不是成员 '全局命名空间'' C:\Program 文件 (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(374):错误 C2146:语法错误:缺少“;”乙 在标识符“value_type”之前 C:\Program Files (x86)\Microsoft 视觉工作室 10.0\VC\INCLUDE\xutility(374) : 错误 C2602: 'std::iterator_traits<_Iter>; ::value_type' 不是 a 的成员 的基类 'std::iterator_traits<_Iter>' 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(374) :参见 'std::iterator 的声明 _traits<_Iter>::value_type' 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(374) : 错误 C2868: 'std::iterator_traits<_Iter>; ::value_type' :非法语法 使用声明;预期的 限定名 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(375) : 错误 C2825: '_Iter': 必须是一个类或 后跟“::”的命名空间 C:\Program Files (x86)\Microsoft 视觉工作室 10.0\VC\INCLUDE\xutility(375):错误 C2039:“difference_type”:不是 “全局命名空间”的成员 C:\Program Files (x86)\Microsoft 视觉工作室 10.0\VC\INCLUDE\xutility(375):错误 C2146:语法错误:缺少“;”乙 在标识符“difference_type”之前 C:\Program Files (x86)\Microsoft 视觉工作室 10.0\VC\INCLUDE\xutility(375) : 错误 C2602: 'std::iterator_traits<_Iter>; ::difference_type' 不是以下成员 的基类 'std::iterator_traits<_Iter>' 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(375) :参见 'std::iterator 的声明 _traits<_Iter>::difference_type' 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(375) : 错误 C2868: 'std::iterator_traits<_Iter>; ::difference_type' :非法语法 用于使用声明;预期的 限定名称 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(377) : 错误 C2825: '_Iter': 必须是一个类或 后跟“::”的命名空间 C:\Program Files (x86)\Microsoft 视觉工作室 10.0\VC\INCLUDE\xutility(377):错误 C2039:“指针”:不是以下成员 '全局命名空间'' C:\Program Files (x86)\微软 Visual Studio 10.0\VC\INCLUDE\xutility(377):错误 C2146:语法错误:缺少“;”乙 在标识符“指针”C:\Program 之前 文件 (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(377) : 错误 C2602: 'std::iterator_traits<_Iter>; ::pointer' 不是基类的成员 'std::iterator_traits<_Iter>' 的类 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(377) :参见 'std::iterator 的声明 _traits<_Iter>::指针' 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(377) : 错误 C2868: 'std::iterator_traits<_Iter>; ::pointer' :非法语法 使用声明;预期的 限定名 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(378) : 错误 C2825: '_Iter': 必须是一个类或 后跟“::”的命名空间 C:\Program Files (x86)\Microsoft 视觉工作室 10.0\VC\INCLUDE\xutility(378):错误 C2039:“引用”:不是成员 ''全局命名空间'' C:\Program 文件 (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(378):错误 C2146:语法错误:缺少“;”乙 在标识符“引用”之前 C:\Program Files (x86)\Microsoft 视觉工作室 10.0\VC\INCLUDE\xutility(378) : 错误 C2602: 'std::iterator_traits<_Iter>; ::reference' 不是基的成员 'std::iterator_traits<_Iter>' 的类 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(378) :参见 'std::iterator 的声明 _traits<_Iter>::参考' 和 [ _Iter=双精度 ] C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(378) : 错误 C2868: 'std::iterator_traits<_Iter>; ::reference' :非法语法 使用声明;预期的 限定名称 和 [ _Iter=双精度 ]

C:\Users\kifcaliph\Desktop\starting 用 c++\Chapter6>*

#include <iostream>
#include <iomanip>

using namespace std;

double distance(double, double);

int main ()
{
    double rate, time, distanceValue;

    cout << fixed << showpoint << setprecision(1);

    cout << "Enter rate" << endl;
    cin >> rate;

    cout << "Enter time" << endl;
    cin >> time;

    distanceValue = distance(rate, time);

    cout << "The distance is " << distanceValue << endl;
}

double distance (double num1, double num2)
{
    return num1 * num2;
}

when I try to compile it I see a long list of errors I don't what's wrong here! also if I change the method from double to int value it works well!! why??

the following error:

*> C:\Users\kifcaliph\Desktop\starting

out with c++\Chapter6>cl 06_012.cpp
06_012 Microsoft (R) 32-bit C/C++
Optimizing Compiler Version
16.00.30319.01 for 80x86 Copyright (C) Microsoft Corporation. All rights
reserved.

cl : Command line warning D9024 :
unrecognized source file type
'06_012', object file assumed
06_012.cpp C:\Program Files
(x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xlocale(323) : warning C4530: C++ exception handler used,
but unwind semantics are not enabled.
Specify /EHsc C:\Program Files
(x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(373) : error C2825: '_Iter': must be a class or
namespace when followed by '::'
06_012.cpp(20) : see reference to class template instantiation
'std::iterator_traits<_Iter>' being
compiled
with
[
_Iter=double
] C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(373) : error C2039: 'iterator_category' : is not a
member of 'global namespace''
C:\Program Files (x86)\Microsoft
Visual Studio
10.0\VC\INCLUDE\xutility(373) : error C2146: syntax error : missing ';' b
efore identifier 'iterator_category'
C:\Program Files (x86)\Microsoft
Visual Studio
10.0\VC\INCLUDE\xutility(373) : error C2602: 'std::iterator_traits<_Iter>
::iterator_category' is not a member
of a base class of
'std::iterator_traits<_Iter>'
with
[
_Iter=double
]
C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(373) : see declaration of 'std::iterator
_traits<_Iter>::iterator_category'
with
[
_Iter=double
] C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(373) : error C2868: 'std::iterator_traits<_Iter>
::iterator_category' : illegal syntax
for using-declaration; expected
qualified-name
with
[
_Iter=double
] C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(374) : error C2825: '_Iter': must be a class or
namespace when followed by '::'
C:\Program Files (x86)\Microsoft
Visual Studio
10.0\VC\INCLUDE\xutility(374) : error C2039: 'value_type' : is not a memb er
of '
global namespace'' C:\Program
Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(374) : error C2146: syntax error : missing ';' b
efore identifier 'value_type'
C:\Program Files (x86)\Microsoft
Visual Studio
10.0\VC\INCLUDE\xutility(374) : error C2602: 'std::iterator_traits<_Iter>
::value_type' is not a member of a
base class of
'std::iterator_traits<_Iter>'
with
[
_Iter=double
]
C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(374) : see declaration of 'std::iterator
_traits<_Iter>::value_type'
with
[
_Iter=double
] C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(374) : error C2868: 'std::iterator_traits<_Iter>
::value_type' : illegal syntax for
using-declaration; expected
qualified-name
with
[
_Iter=double
] C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(375) : error C2825: '_Iter': must be a class or
namespace when followed by '::'
C:\Program Files (x86)\Microsoft
Visual Studio
10.0\VC\INCLUDE\xutility(375) : error C2039: 'difference_type' : is not a
member of 'global namespace''
C:\Program Files (x86)\Microsoft
Visual Studio
10.0\VC\INCLUDE\xutility(375) : error C2146: syntax error : missing ';' b
efore identifier 'difference_type'
C:\Program Files (x86)\Microsoft
Visual Studio
10.0\VC\INCLUDE\xutility(375) : error C2602: 'std::iterator_traits<_Iter>
::difference_type' is not a member of
a base class of
'std::iterator_traits<_Iter>'
with
[
_Iter=double
]
C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(375) : see declaration of 'std::iterator
_traits<_Iter>::difference_type'
with
[
_Iter=double
] C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(375) : error C2868: 'std::iterator_traits<_Iter>
::difference_type' : illegal syntax
for using-declaration; expected
qualified-name
with
[
_Iter=double
] C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(377) : error C2825: '_Iter': must be a class or
namespace when followed by '::'
C:\Program Files (x86)\Microsoft
Visual Studio
10.0\VC\INCLUDE\xutility(377) : error C2039: 'pointer' : is not a member of
'
global namespace'' C:\Program Files
(x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(377) : error C2146: syntax error : missing ';' b
efore identifier 'pointer' C:\Program
Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(377) : error C2602: 'std::iterator_traits<_Iter>
::pointer' is not a member of a base
class of 'std::iterator_traits<_Iter>'
with
[
_Iter=double
]
C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(377) : see declaration of 'std::iterator
_traits<_Iter>::pointer'
with
[
_Iter=double
] C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(377) : error C2868: 'std::iterator_traits<_Iter>
::pointer' : illegal syntax for
using-declaration; expected
qualified-name
with
[
_Iter=double
] C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(378) : error C2825: '_Iter': must be a class or
namespace when followed by '::'
C:\Program Files (x86)\Microsoft
Visual Studio
10.0\VC\INCLUDE\xutility(378) : error C2039: 'reference' : is not a membe r
of '`global namespace'' C:\Program
Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(378) : error C2146: syntax error : missing ';' b
efore identifier 'reference'
C:\Program Files (x86)\Microsoft
Visual Studio
10.0\VC\INCLUDE\xutility(378) : error C2602: 'std::iterator_traits<_Iter>
::reference' is not a member of a base
class of 'std::iterator_traits<_Iter>'
with
[
_Iter=double
]
C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(378) : see declaration of 'std::iterator
_traits<_Iter>::reference'
with
[
_Iter=double
] C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC\INCLUDE\xutility(378) : error C2868: 'std::iterator_traits<_Iter>
::reference' : illegal syntax for
using-declaration; expected
qualified-name
with
[
_Iter=double
]

C:\Users\kifcaliph\Desktop\starting
out with c++\Chapter6>*

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

新一帅帅 2024-11-23 01:07:32

猜测(因为您没有费心向我们实际显示相关错误)std::distance:: 之间存在歧义错误distance 由于您的 using 指令而发挥作用。完全限定 ::distance 以强制调用您的函数,或者摆脱 using 指令并完全限定所有内容或为命名空间 std。

此外,您在 main 最后一行中将 distanceValue 拼写为 distancValue

I'm guessing (since you didn't bother to actually show us the errors in question) that there's an ambiguity error between std::distance and ::distance that's coming into play because of your using directive. Fully qualify ::distance to force your function to be called, or get rid of the using directive and either fully qualify everything or add a using declaration for each of the symbols inside namespace std that you intend to use.

Also, you misspelled distanceValue as distancValue in the last line of main.

将军与妓 2024-11-23 01:07:32

您正在看到 using namespace std; 的一个不幸的副作用。有一个名为 std::distance 的函数;由于您使用了 using namespace std; 它优先于您自己的 distance 函数。

解决方案是不使用 using namespace std;,而是单独引用 std::coutstd::set precision 等。您还可以从 std 导入特定标识符:

using std::cout;
using std::setprecision;
using std::fixed;
using std::showpoint;
using std::cin;
using std::endl;

或者您可以继续使用 using namespace std;,但使用 显式请求您自己的 distance ::distance - 这专门请求与空命名空间的距离(不推荐;如果向 std 添加任何内容,这可能会再次中断)。

另外,distancValue 是一个拼写错误。

You are seeing an unfortunate side effect of using namespace std;. There is a function called std::distance; since you used using namespace std; it takes precedence over your own distance function.

The solution is to not use using namespace std;, and instead reference std::cout, std::setprecision, etc individually. You can also import specific identifiers from std:

using std::cout;
using std::setprecision;
using std::fixed;
using std::showpoint;
using std::cin;
using std::endl;

Or you can continue using using namespace std;, but explicitly request your own distance with ::distance - this requests distance from the empty namespace specifically (not recommended; if anything is added to std this could break again).

Also, distancValue is a typo.

风吹雪碎 2024-11-23 01:07:32

std::distance 和您的 ::distance 之间会存在歧义 - 要么完全限定调用,要么不包含整个 std > 命名空间。

It'll be ambiguity between std::distance and your ::distance - either fully qualify the call, or dont include the whole of the std namespace.

晚风撩人 2024-11-23 01:07:32

错误是你写的是:

cout << "The distance is " << distancValue << endl;

而不是:

cout << "The distance is " << distanceValue << endl;

你看,你错过的距离中有一个小“a”,修复它,它会工作得很好。

下次要小心:)

The error is that you wrote:

cout << "The distance is " << distancValue << endl;

instead of:

cout << "The distance is " << distanceValue << endl;

You see, there is a little 'a' in distance that you missed, fix it and it will work well.

And be careful next time :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文