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

#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>*

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

Related

Undefined type and Undeclared identifiers in xstring

I've been creating a class that defines an integer in a base other than ten. Today when I tried to compile I got a long list of errors in xstring which seemed to mostly be undefined type errors or undeclared identifier errors. I was wondering if this is a result of forgetting to include some file or me misusing a string.
intBase.cpp (the class where I would have mishandled a string)
#include "intBase.h"
intBase::intBase(int b)
{
base = b;
num = 0;
}
intBase::intBase(std::string n, int b)
{
base = b;
setNum(n);
}
int intBase::getBase() const
{
return base;
}
void intBase::setBase(int b)
{
base = b;
}
std::string intBase::getNum() const
{
int expo = 0, n, numTemp = num;
std::string result = "";
char ch;
while (num > int(pow(base, expo)))
{
expo++;
}
expo--;
while (expo >= 0)
{
n = int(numTemp / pow(base, expo));
ch = convert(n, false);
numTemp -= int(n * pow(base, expo));
result += ch;
expo--;
}
return result;
}
void intBase::setNum(std::string n)
{
char ch;
int expo, temp;
num = 0;
expo = 0;
while (n.size() > 0)
{
ch = n[n.size() - 1];
temp = convert(ch, true);
num += int(temp * pow(base, expo));
expo++;
n = n.substr(0, n.size() - 1);
}
}
int intBase::getNumTen() const
{
return num;
}
void intBase::setNumTen(int n)
{
num = n;
}
intBase intBase::add(const intBase& iBase) const
{
intBase IB(1);
if (getBase() > iBase.getBase())
{
IB.setBase(base);
}
else
{
IB.setBase(base);
}
IB.setNumTen(getNumTen() + iBase.getNumTen());
return IB;
}
intBase intBase::operator+(const intBase& iBase) const
{
return add(iBase);
}
std::ostream& operator<<(std::ostream& output, const intBase& iBase)
{
output << iBase.getNum();
return output;
}
// takes a character and returns its base ten conversion if bool is true
// takes a base-ten integer and returns its character conversion if bool is false
int intBase::convert(int x, bool letter) const
{
int result;
if (letter)
{
if (x >= 48 && x <= 57)
result = x - 48;
else if (x >= 97 && x <= 122)
result = x - 87;
else if (x >= 65 && x <= 90)
result = x - 29;
else
result = -1;
}
else
{
if (x >= 0 && x <= 9)
result = x + 48;
else if (x >= 10 && x <= 35)
result = x + 87;
else if (x >= 36 && x <= 61)
result = x + 29;
else
result = -1;
}
return result;
}
intBase.h
#pragma once
#include <string>
#include <cmath>
class intBase
{
public:
intBase(int b);
intBase(std::string n, int b);
int getBase() const;
void setBase(int b);
std::string getNum() const;
void setNum(std::string n);
int getNumTen() const;
void setNumTen(int n);
intBase add(const intBase& iBase) const;
intBase operator+(const intBase& iBase) const;
friend std::ostream& operator<<(std::ostream& output, const intBase& iBase);
private:
int convert(int x, bool y) const;
int num, base;
};
test.cpp
#include <iostream>
#include "intBase.h"
#include <string>
using namespace std;
int main()
{
int base;
string numTemp;
cout << "enter the base of your number: ";
cin >> base;
intBase num(base);
cout << "enter a number in base " << base << ": ";
cin >> numTemp;
num.setNum(numTemp);
cout << num.getNumTen() << endl;
cout << num << endl;
cout << num + num << endl;
system("pause");
return 0;
}
error log
1>------ Build started: Project: numBaseTest, Configuration: Debug Win32 ------
1>intBase.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,23): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::_Insert_string<char,std::char_traits<char>,unsigned int>(std::basic_ostream<char,std::char_traits<char>> &,const _Elem *const ,const _SizeT)' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : with
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : [
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : _Elem=char,
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : _SizeT=unsigned int
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(4519): message : ]
1>E:\college\numBaseTest\numBaseTest\numBaseTest\intBase.cpp(104): message : see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>,std::allocator<char>>(std::basic_ostream<char,std::char_traits<char>> &,const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,1): error C2065: 'iostate': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,31): error C2146: syntax error: missing ';' before identifier '_State'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,31): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,49): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(435,1): error C2065: 'goodbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(438,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(441,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(444,29): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(444,1): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(444,36): error C2146: syntax error: missing ';' before identifier '_Ok'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(444,36): error C3861: '_Ok': identifier not found
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(446,10): error C2065: '_Ok': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(447,9): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(447,28): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(447,1): error C2065: 'badbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(450,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(450,39): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(450,1): error C2065: 'adjustfield': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(450,64): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(450,1): error C2065: 'left': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(452,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(452,1): error C2660: 'std::_Narrow_char_traits<char,int>::eq_int_type': function does not take 1 arguments
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(401,38): message : see declaration of 'std::_Narrow_char_traits<char,int>::eq_int_type'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(453,21): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(453,40): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(453,1): error C2065: 'badbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(459,13): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(459,32): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(459,1): error C2065: 'goodbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(460,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(461,13): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(461,32): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(461,1): error C2065: 'badbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(464,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(464,1): error C2660: 'std::_Narrow_char_traits<char,int>::eq_int_type': function does not take 1 arguments
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(401,38): message : see declaration of 'std::_Narrow_char_traits<char,int>::eq_int_type'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(465,21): error C2065: '_State': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(465,40): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(465,1): error C2065: 'badbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(471,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(472,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(472,9): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(472,1): error C2065: 'badbit': undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(475,1): error C2027: use of undefined type 'std::basic_ostream<char,std::char_traits<char>>'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\iosfwd(216): message : see declaration of 'std::basic_ostream<char,std::char_traits<char>>'
1>test.cpp
1>Generating Code...
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xstring(475,20): error C2065: '_State': undeclared identifier
1>Done building project "numBaseTest.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Thank you for all your help in advance.
You are missing an include to sstream. Add it to the top of intBase.h:
#pragma once
#include <sstream>
#include <string>
#include <cmath>

Seeming error in Visual C++ 2017 ( ver 15.9.3 )

Have the following code which compiles and runs fine in Visual C++ 2013, and the C++14 standard on http://cpp.sh. However, on Visual C++ 2017 ( ver 15.9.3 ), it gives an error message... which is maybe a bug?
Code is:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
vector<long> v1 = {1}, v2 = {9};
swap<vector<long>>(v1, v2);
cout << "v1.front(): " << v1.front() << endl;
cout << "v2.front(): " << v2.front() << endl;
return 0;
}
I should note that if I comment out swap() function, then it compiles and runs ok. The error messages are all resulting from the call to swap().
Error messages are:
maptest.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2048): error C2039: '_Alloc': is not a member of 'std::vector<long,std::allocator<_Ty>>'
with
[
_Ty=long
]
maptest.cpp(9): note: see declaration of 'std::vector<long,std::allocator<_Ty>>'
with
[
_Ty=long
]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2096): note: see reference to class template instantiation 'std::_Vb_iter_base<_Alvbase_wrapped>' being compiled
with
[
_Alvbase_wrapped=std::vector<long,std::allocator<long>>
]
maptest.cpp(11): note: see reference to class template instantiation 'std::_Vb_reference<std::vector<long,std::allocator<_Ty>>>' being compiled
with
[
_Ty=long
]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2048): error C2061: syntax error: identifier '_Alloc'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2049): error C2065: '_Alvbase': undeclared identifier
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2049): error C2923: 'std::allocator_traits': '_Alvbase' is not a valid template type argument for parameter '_Alloc'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2049): error C2955: 'std::allocator_traits': use of class template requires template argument list
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xmemory0(903): note: see declaration of 'std::allocator_traits'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2049): error C2039: 'size_type': is not a member of 'std::allocator_traits<_Alloc>'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2049): error C2061: syntax error: identifier 'size_type'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2050): error C2065: '_Alvbase': undeclared identifier
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2050): error C2923: 'std::allocator_traits': '_Alvbase' is not a valid template type argument for parameter '_Alloc'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2050): error C2955: 'std::allocator_traits': use of class template requires template argument list
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xmemory0(903): note: see declaration of 'std::allocator_traits'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2050): error C2039: 'difference_type': is not a member of 'std::allocator_traits<_Alloc>'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2050): error C2061: syntax error: identifier 'difference_type'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2051): error C2065: '_Alvbase': undeclared identifier
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2051): error C2923: 'std::_Rebind_alloc_t': '_Alvbase' is not a valid template type argument for parameter '_Alloc'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2058): error C2061: syntax error: identifier '_Sizet'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2065): error C2061: syntax error: identifier '_Sizet'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2072): error C2061: syntax error: identifier '_Sizet'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2089): error C3646: '_Myoff': unknown override specifier
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2089): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
std::swap is overloaded for std::vector as
template< class T, class Alloc>
void swap(vector<T,Alloc>& lhs, vector<T,Alloc>& rhs);
The correct template arguments for your given vector would be T=long, Alloc = std::allocator<long>. The proper way to call swap (or almost any other templated function) on an std::vector is to simply drop the explicit template argument specification altogether and let template argument deduction do its work.

error: C2039: 'pointer' : is not a member of 'QJsonObject::iterator'

I'm having a weird error when trying to use 'QJsonObject::iterator' with MSVC2013.
I have the following example:
#include <QCoreApplication>
#include <QJsonObject>
#include <QDebug>
#include <algorithm>
void processValue(QJsonValue value) {
qDebug() << value.toString();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QJsonObject jsonObject;
jsonObject.insert("a", "A");
jsonObject.insert("b", "B");
jsonObject.insert("c", "C");
jsonObject.insert("d", "D");
jsonObject.insert("e", "E");
std::for_each (jsonObject.begin(), jsonObject.end(), processValue);
return a.exec();
}
This code compiles and works as expected with MSVC2008 (cross-compiling to WinCE) and MinGW, but not with MSVC2013. In all cases, I'm using Qt 5.5.1.
The error message is:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : error C2039: 'pointer' : is not a member of 'QJsonObject::iterator'
c:\qt\qt5.5.1msvc\5.5\msvc2013\include\qtcore\qjsonobject.h(96) : see declaration of 'QJsonObject::iterator'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(584) : see reference to class template instantiation 'std::iterator_traits<_InIt>' being compiled
with
[
_InIt=QJsonObject::iterator
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\algorithm(31) : see reference to function template instantiation 'void std::_Debug_range<_InIt>(_InIt,_InIt,std::_Dbfile_t,std::_Dbline_t)' being compiled
with
[
_InIt=QJsonObject::iterator
]
..\QJsonObjectIteratorIssue\main.cpp(21) : see reference to function template instantiation '_Fn1 std::for_each<QJsonObject::iterator,void(__cdecl *)(QJsonValue)>(_InIt,_InIt,_Fn1)' being compiled
with
[
_Fn1=void (__cdecl *)(QJsonValue)
, _InIt=QJsonObject::iterator
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : error C2146: syntax error : missing ';' before identifier 'pointer'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : error C2602: 'std::iterator_traits<_InIt>::pointer' is not a member of a base class of 'std::iterator_traits<_InIt>'
with
[
_InIt=QJsonObject::iterator
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : see declaration of 'std::iterator_traits<_InIt>::pointer'
with
[
_InIt=QJsonObject::iterator
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : error C2868: 'std::iterator_traits<_InIt>::pointer' : illegal syntax for using-declaration; expected qualified-name
with
[
_InIt=QJsonObject::iterator
]
Am I doing anything wrong here, that just happen to work by chance on the 2 other compilers?
Use 5.6 or backport this: https://code.qt.io/cgit/qt/qtbase.git/commit/?id=4a318a61824216ac499ff8b0b0c55dea90501005
QJsonObject::(const_)iterator: add pointer typedef
Otherwise they're unusable with std::algorithms or anything else
that requires iterator_traits.

distance calculation error in c++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int square(int a){
return a*a;
}
struct Point{
int x,y;
};
int distance (const Point& a,const Point& b){
int k=(int) sqrt((float)((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y)));
return k;
}
int main(){
vector<Point>a(10);
for (int i=0;i<10;i++){
cin>>a[i].x>>a[i].y;
}
int s=0;
int s1;
int k=0;
for (int i=1;i<10;i++){
s+=square(distance(a[0],a[i]));
}
for (int i=1;i<10;i++){
s1=0;
for (int j=0;j<10;j++){
s1+=square(distance(a[i],a[j]));
if (s1<s) { s=s1; k=i;}
}
}
cout<<k<<"Points are:";
cout<<a[k].x;
cout<<a[k].y;
return 0;
}
i have following code but here is list of errors
1>------ Build started: Project: distance, Configuration: Debug Win32 ------
1> distance.cpp
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(373): error C2039: 'iterator_category' : is not a member of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(9) : see declaration of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(30) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(373): error C2146: syntax error : missing ';' before identifier 'iterator_category'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(373): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\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>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(373) : see declaration of 'std::iterator_traits<_Iter>::iterator_category'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\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
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(374): error C2039: 'value_type' : is not a member of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(9) : see declaration of 'Point'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(374): error C2146: syntax error : missing ';' before identifier 'value_type'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(374): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\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>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(374) : see declaration of 'std::iterator_traits<_Iter>::value_type'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\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
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(375): error C2039: 'difference_type' : is not a member of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(9) : see declaration of 'Point'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(375): error C2146: syntax error : missing ';' before identifier 'difference_type'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(375): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\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>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(375) : see declaration of 'std::iterator_traits<_Iter>::difference_type'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\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
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(377): error C2039: 'pointer' : is not a member of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(9) : see declaration of 'Point'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(377): error C2146: syntax error : missing ';' before identifier 'pointer'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(377): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\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>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(377) : see declaration of 'std::iterator_traits<_Iter>::pointer'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(377): error C2868: 'std::iterator_traits<_Iter>::pointer' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(378): error C2039: 'reference' : is not a member of 'Point'
1> d:\c++_algorithms\distance\distance\distance.cpp(9) : see declaration of 'Point'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(378): error C2146: syntax error : missing ';' before identifier 'reference'
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(378): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\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>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(378) : see declaration of 'std::iterator_traits<_Iter>::reference'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(378): error C2868: 'std::iterator_traits<_Iter>::reference' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This is caused in part by your use of using namespace std;.
There is a function in the standard library, std::distance, that is used to compute the distance between two iterators into a container. That function is being selected during overload resolution instead of your distance function.
If you don't use using namespace std;, you won't have this problem. Your use of using namespace std; brings all of the names from the std namespace into the global namespace. This is bad because there are a lot of common names in namespace std and it can cause convoluted issues during name lookup, as you've discovered here.
In general, prefer not to use using namespace, especially not at file scope. It's far easier and clearer in the long run to qualify each of the names that you want to use.
#James is right in that getting rid of using namespace std will make this problem go away. But that's not exactly what the actual problem is.
The problem comes from the way names are resolved in C++. C++ uses a very aggressive approach to resolving name lookups when resolving the names of free functions if no suitable class member was found. This is called Argument-dependent name lookup (or ADL), sometimes referred to as Koenig lookup. And at the same time, it is one of the things makes C++ so powerful, but also so deadly and confounding.
Here's the basic problem, simplified.
You call vector::operator[] which returns a vector::reference or a vector::const_reference. You take this return value and call distance() unqualified.
Because distance wasn't found in and applicable class, ADL kicks in, which goes out and gets too many potential matching names.
ADL then goes through the list of candidates and picks "the best match". In this case, the reference that operator[] returns is a closer match to the type expected by std::distance than by your distance, although both are compatible, so std::distance is selected.
Several ways to solve this problem:
Don't using namespace std. Instead, using just the peices you want.
Quantify explicitly which distance you refer to using the scope resolution operator: ::distance()
Change distance to take pointers instead of const &. This effectively turns off ADL.:
Example:
int distance(const Point* a,const Point* b)
s+=square(distance(&a[0],&a[1]));
Additional Resources:
"A Modest Proposal: Fixing ADL (revision 2)" by Herb Sutter

implementation of hash table [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 12 years ago.
i have following code
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <hash_map>
#include <string>
#include <iterator>
#include <ostream>
using namespace std;
struct Equal
{
bool operator()(const char *s1,const char *s2)const
{
return std::strcmp(s1,s2)==0;
}
};
typedef std::hash_multimap<const char*,int,hash<const char*>,Equal>map_type;
void lookup(const map_type&Map,const char *str){
cout<<str<<":";
pair<map_type::const_iterator ,map_type::const_iterator>p=Map.equal_range(str);
for (map_type::const_iterator i=p.first;i!=p.second;++i)
cout << (*i).second<<" ";
}
int maain(){
map_type m;
m.insert(map_type::value_type("H", 1));
m.insert(map_type::value_type("H", 2));
m.insert(map_type::value_type("C", 12));
m.insert(map_type::value_type("C", 13));
m.insert(map_type::value_type("O", 16));
m.insert(map_type::value_type("O", 17));
m.insert(map_type::value_type("O", 18));
m.insert(map_type::value_type("I", 127));
lookup(M,"I");
lookup(M,"0");
lookup(M,"Rn");
return 0;
}
but here is errors
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2903: 'rebind' : symbol is neither a class template nor a function template
1> c:\program files\microsoft visual studio 10.0\vc\include\xhash(170) : see reference to class template instantiation 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
1> with
1> [
1> _Kty=const char *,
1> _Ty=int,
1> _Tr=std::hash<const char *>,
1> _Alloc=Equal,
1> _Mfl=true
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\hash_map(273) : see reference to class template instantiation 'std::_Hash<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Hmap_traits<const char *,int,std::hash<const char *>,Equal,true>
1> ]
1> c:\users\7\documents\visual studio 2010\projects\hash_tables\hash_tables\hash_tables.cpp(22) : see reference to class template instantiation 'stdext::hash_multimap<_Kty,_Ty,_Tr,_Alloc>' being compiled
1> with
1> [
1> _Kty=const char *,
1> _Ty=int,
1> _Tr=std::hash<const char *>,
1> _Alloc=Equal
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2039: 'rebind' : is not a member of 'Equal'
1> c:\users\7\documents\visual studio 2010\projects\hash_tables\hash_tables\hash_tables.cpp(11) : see declaration of 'Equal'
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2143: syntax error : missing ';' before '<'
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2039: 'other' : is not a member of '`global namespace''
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(27): error C2238: unexpected token(s) preceding ';'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(180): error C2039: 'bucket_size' : is not a member of 'std::hash<_Kty>'
1> with
1> [
1> _Kty=const char *
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(180): error C2065: 'bucket_size' : undeclared identifier
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2039: 'allocator_type' : is not a member of 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>'
1> with
1> [
1> _Kty=const char *,
1> _Ty=int,
1> _Tr=std::hash<const char *>,
1> _Alloc=Equal,
1> _Mfl=true
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2146: syntax error : missing ',' before identifier 'allocator_type'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2065: 'allocator_type' : undeclared identifier
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(186): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(187): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(188): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(189): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(190): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(191): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(192): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(193): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(197): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2146: syntax error : missing ';' before identifier 'iterator'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C3254: 'std::_Hash<_Traits>' : class contains explicit override 'type' but does not derive from an interface that contains the function declaration
1> with
1> [
1> _Traits=std::_Hmap_traits<const char *,int,std::hash<const char *>,Equal,true>
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2838: 'type' : illegal qualified name in member declaration
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2208: 'std::iterator' : no members defined using this type
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.96
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
please help
You have to look at each complier error and fix them one at a time. We can't take a file of code and fix each error. Learning to live and even on occasion love the abusive spouse that is your complier is part of the process.
All your errors seem related to the fact that you're not passing the right template arguments to hash_multimap. Take a look at hash_multimap's documentation. The last parameter, Ăˆqual` should be the allocator, not a function object, check out the 1st error:
c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2903: 'rebind' : symbol is neither a class template nor a function template
c:\program files\microsoft visual studio 10.0\vc\include\xhash(170) : see reference to class template instantiation 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
with
[
_Kty=const char *,
_Ty=int,
_Tr=std::hash,
_Alloc=Equal,
_Mfl=true
]
Edit:
If you've looked at SGI's hash_multimap documentation, you'll notice that the hash_map and hash_multimap classes are different between vendors because they are not standard. The next version of the standard comprises standard hashed containers. In the mean time, well, you'll have to write non portable code!