开发者

I can't print a double value in the following code?

#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 " << dist开发者_开发知识库anceValue << 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>*


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.


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.


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


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜