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.
Related
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.
I'm in the process of porting some of my Linux programs to Windows. I would like to make sure my codes compile with both MinGW and Visual C++ 10 without too much #define and #ifdef magic. I've managed to work out most of the problems, except this one. I have a code similar to the following, which compiles fine with g++ 4.6.3 but not with Visual C++ 10:
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iterator>
typedef unsigned char UINT8;
const UINT8 MAX_COLL = 10;
template< typename BaseT, typename ExpT >
struct fopow {
BaseT operator() ( BaseT base, ExpT exp ) const {
return std::pow( base, exp );
}
};
int main() {
using namespace std;
using namespace std::placeholders;
array<int, MAX_COLL> intCollection_init = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
vector<int> intCollection( intCollection_init.begin(), intCollection_init.end() );
transform( intCollection.begin(), intCollection.end(),
ostream_iterator<int>( cout, " " ),
bind( fopow<int, int>(), 3, _1 )
);
cout << endl;
transform( intCollection.begin(), intCollection.end(),
ostream_iterator<int>( cout, " " ),
bind( fopow<int, int>(), _1, 3 )
);
cout << endl;
}
The compiler output is not even remotely intelligible to me:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
Test2.cpp C:\Program Files\Microsoft Visual Studio
10.0\VC\INCLUDE\xxresult(28) : error C2903: 'result' : symbol is neither a class template nor a function template
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : see reference to class tem plate instantiation 'std::tr1::_Result_type2<__formal,_Fty,_Arg0,_Arg1>' being compiled
with
[
__formal=false,
_Fty=fopow<int,int>,
_Arg0=std::tr1::_Nil &,
_Arg1=std::tr1::_Nil &
]
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(597) : see reference to class te mplate instantiation 'std::tr1::_Result_of2<_Fty,_Farg0,_Farg1>' being compiled
with
[
_Fty=fopow<int,int>,
_Farg0=std::tr1::_Nil &,
_Farg1=std::tr1::_Nil &
]
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xrefwrap(28) : see reference to class tem plate instantiation 'std::tr1::_Result_of<_Ty>' being compiled
with
[
_Ty=fopow<int,int> (std::tr1::_Nil &,std::tr1::_Nil &)
]
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxbind1(273) : see reference to class tem plate instantiation 'std::tr1::result_of<_Fty>' being compiled
with
[
_Fty=fopow<int,int> (std::tr1::_Nil &,std::tr1::_Nil &)
]
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxbind0(10) : see reference to class temp late instantiation 'std::tr1::_Bind2<_Callable,_Arg0,_Arg1>::_Return<_Barg0,_Barg1,_Barg2,_Barg3,_Barg4,_B arg5,_Barg6,_Barg7,_Barg8,_Barg9>' being compiled
with
[
_Callable=std::tr1::_Callable_obj<fopow<int,int>,false>,
_Arg0=int,
_Arg1=std::tr1::_Ph<1>,
_Barg0=std::tr1::_Nil &,
_Barg1=std::tr1::_Nil &,
_Barg2=std::tr1::_Nil &,
_Barg3=std::tr1::_Nil &,
_Barg4=std::tr1::_Nil &,
_Barg5=std::tr1::_Nil &,
_Barg6=std::tr1::_Nil &,
_Barg7=std::tr1::_Nil &,
_Barg8=std::tr1::_Nil &,
_Barg9=std::tr1::_Nil &
]
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\functional(408) : see reference to class template instantiation 'std::tr1::_Bind_base<_Ret,_BindN>' being compiled
with
[
_Ret=std::tr1::_Notforced,
_BindN=std::tr1::_Bind2<std::tr1::_Callable_obj<fopow<int,int>,false>,int,std::tr1::_Ph<1>>
]
Test2.cpp(26) : see reference to class template instantiation 'std::tr1::_Bind_fty<_Fty,_Ret,_Bind N>' being compiled
with
[
_Fty=fopow<int,int>,
_Ret=std::tr1::_Notforced,
_BindN=std::tr1::_Bind2<std::tr1::_Callable_obj<fopow<int,int>,false>,int,std::tr1::_Ph<1>>
] C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(28) : error C2039: 'result' : is not a m ember of 'fopow<BaseT,ExpT>'
with
[
BaseT=int,
ExpT=int
] C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(28) : error C2143: syntax error : missin g ';' before '<' C:\Program Files\Microsoft Visual Studio
10.0\VC\INCLUDE\xxresult(28) : error C4430: missing type specifie r - int assumed. Note: C++ does not support default-int C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(28) : error C2039: 'type' : is not a mem ber of '`global namespace'' C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(28) : error C2238: unexpected token(s) p receding ';' C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : error C2039: '_Type' : is not a me mber of 'std::tr1::_Result_type2<__formal,_Fty,_Arg0,_Arg1>'
with
[
__formal=false,
_Fty=fopow<int,int>,
_Arg0=std::tr1::_Nil &,
_Arg1=std::tr1::_Nil &
] C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : error C2146: syntax error : missin g ';' before identifier '_Type' C:\Program Files\Microsoft Visual Studio
10.0\VC\INCLUDE\xxresult(40) : error C4430: missing type specifie r - int assumed. Note: C++ does not support default-int C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : error C2602: 'std::tr1::_Result_of 2<_Fty,_Farg0,_Farg1>::_Type' is not a member of a base class of 'std::tr1::_Result_of2<_Fty,_Farg0,_Farg1
>'
with
[
_Fty=fopow<int,int>,
_Farg0=std::tr1::_Nil &,
_Farg1=std::tr1::_Nil &
]
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : see declaration of 'std::t r1::_Result_of2<_Fty,_Farg0,_Farg1>::_Type'
with
[
_Fty=fopow<int,int>,
_Farg0=std::tr1::_Nil &,
_Farg1=std::tr1::_Nil &
] C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xxresult(40) : error C2868: 'std::tr1::_Result_of 2<_Fty,_Farg0,_Farg1>::_Type' : illegal syntax for using-declaration; expected qualified-name
with
[
_Fty=fopow<int,int>,
_Farg0=std::tr1::_Nil &,
_Farg1=std::tr1::_Nil &
]
I think the root of the problem is bind() or transform() but I don't know what exactly is wrong with the code (as it compiles fine without a single warning with g++). Anyone knows how to fix my code without resorting to prepocessor magics?
std::bind is not what you think it is in VS2010: it is in fact std::tr1::bind, and uses std::tr1::result_of, which is much less powerful than the compiler-assisted std::result_of mandated by the C++11 standard.
In particular, don't try to use std::bind with lambda-expressions in VS10.
You can resolve this here by having fopow inherit std::binary_function<BaseT, ExpT, BaseT>.
Add typedef BaseT result_type; to the definition of fopow. The template hackery that was needed in the days before decltype is getting lost trying to figure out what the return type of fopow::operator() is. (And, yes, you're right: it's getting lost in the bind() call; specifically, the first one.)
Or, if you like, you can use the fix suggested by #ecatmur: derive from std::binary_function<BaseT, ExpT, BaseT>. That will also add result_type to the template, as well as first_argument_type and second_argument_type. bind doesn't need the argument types.
#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 :)
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
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!