我无法在以下代码中打印双精度值?
#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''
global namespace'' C:\Program
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 '
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''
global namespace'' C:\Program Files
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
'
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜测(因为您没有费心向我们实际显示相关错误)
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 namespacestd
that you intend to use.Also, you misspelled
distanceValue
asdistancValue
in the last line ofmain
.您正在看到
using namespace std;
的一个不幸的副作用。有一个名为std::distance
的函数;由于您使用了using namespace std;
它优先于您自己的distance
函数。解决方案是不使用
using namespace std;
,而是单独引用std::cout
、std::set precision
等。您还可以从std
导入特定标识符:或者您可以继续使用
using namespace std;
,但使用显式请求您自己的
- 这专门请求与空命名空间的距离(不推荐;如果向distance
::distancestd
添加任何内容,这可能会再次中断)。另外,
distancValue
是一个拼写错误。You are seeing an unfortunate side effect of
using namespace std;
. There is a function calledstd::distance
; since you usedusing namespace std;
it takes precedence over your owndistance
function.The solution is to not use
using namespace std;
, and instead referencestd::cout
,std::setprecision
, etc individually. You can also import specific identifiers fromstd
:Or you can continue using
using namespace std;
, but explicitly request your owndistance
with::distance
- this requests distance from the empty namespace specifically (not recommended; if anything is added tostd
this could break again).Also,
distancValue
is a typo.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 thestd
namespace.错误是你写的是:
而不是:
你看,你错过的距离中有一个小“a”,修复它,它会工作得很好。
下次要小心:)
The error is that you wrote:
instead of:
You see, there is a little 'a' in distance that you missed, fix it and it will work well.
And be careful next time :)