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

时间:2011-06-17 23:34:46

标签: c++ double cout

#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值,它也可以正常工作!!为什么??

以下错误:

* GT; C:\ Users \用户kifcaliph \桌面\起始

  

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

     

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

     

C:\用户\ kifcaliph \桌面\开始   用c ++ \ Chapter6&gt; *

4 个答案:

答案 0 :(得分:6)

猜测(因为你真的没有真正向我们展示有问题的错误),std::distance::distance之间存在歧义错误因为你的使用指令而玩。完全限定::distance以强制调用函数,或者删除using指令并完全限定所有内容,或者为要使用的名称空间std内的每个符号添加using声明。

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

答案 1 :(得分:4)

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

解决方案是不使用using namespace std;,而是单独引用std::coutstd::setprecision等。您还可以从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 1}}这可能会再次破裂。)

此外,distancValue是一个错字。

答案 2 :(得分:0)

std::distance与您的::distance之间存在歧义 - 要么完全符合要求,要么不要包含整个std命名空间。

答案 3 :(得分:0)

错误是你写的:

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

而不是:

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

你知道,你错过了一点'距离',修复它并且效果会很好。

下次要小心:)