Unexpected template instantiation leading to a compile error - c++

I just wonder why my code cannot be compiled. Is the below okay? I'm trying to declare a simple class with Category1 and Category2 typedef-s.
Category1 typedef compiles fine but Category2 one does not.
It seems that Category2 typedef cannot be compiled because class iterator_traits<> is instantiated despite the surrounding class X is not instantiated... Seems very confusing for me.
#include <iterator>
template <class GetterFunType>
struct X {
GetterFunType containerGetterFun;
// works
typedef typename std::iterator_traits<typename GetterFunType::iterator>::iterator_category Category1;
// compile error - std::iterator_traits<> is instantiated with type 'unknown'
typedef typename std::iterator_traits<
decltype(containerGetterFun().begin())>::iterator_category Category2;
X(GetterFunType _containerGetterFun) : containerGetterFun(_containerGetterFun) { }
};
Note that I do not need to instantiate class X to get the following errors (the above is the complete compilation unit).
In Visual Studio 2012 I'm getting this:
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2146: syntax error : missing ';' before identifier 'iterator_category'
1> c:\data\fsl\apif_src_review\apif_src\systemns.cpp(11) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled
1> with
1> [
1> _Iter=unknown
1> ]
1> c:\data\fsl\apif_src_review\apif_src\systemns.cpp(14) : see reference to class template instantiation 'X<GetterFunType>' being compiled
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C3254: 'std::iterator_traits<_Iter>' : class contains explicit override 'iterator_category' but does not derive from an interface that contains the function declaration
1> with
1> [
1> _Iter=unknown
1> ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2838: 'iterator_category' : illegal qualified name in member declaration
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): 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=unknown
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364) : see declaration of 'std::iterator_traits<_Iter>::iterator_category'
1> with
1> [
1> _Iter=unknown
1> ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=unknown
1> ]
In xutility(364) there is:
template<class _Iter>
struct iterator_traits
{ // get traits from iterator _Iter
typedef typename _Iter::iterator_category iterator_category;
typedef typename _Iter::value_type value_type;
typedef typename _Iter::difference_type difference_type;
typedef difference_type distance_type; // retained
typedef typename _Iter::pointer pointer;
typedef typename _Iter::reference reference;
};
My case is that I want to declare a class that gets a lambda in constructor. The lambda is expected to return a reference to a container. And I need to determine wheter the returned container has a random-access iterator. But I got stuck with this compilation error. Thank you for explanation!

I was able to compile the same code without any errors using gcc 5.3.1, with -std=c++11
Your compiler is a relatively old compiler that does not support the current C++1x standard. Switching to another compiler is the only option I can see here, if you need to use modern C++ features.

I see that the code is okay but there are some Visual Studio 2012 limitations not allowing to compile. At least in MSVS 2015 it works, same as for gcc 5.3.1. Thanks, friends!

Related

vector in map in map

typedef vector<string> tdv_Str;
typedef map<string, tdv_Str, CaseI> tdm_StrList; // var name, possible values
typedef map<short, tdm_StrList> tdm_VarList; // type, var list
*CaseI is string map sorting with case insensitive.
When I check if the key of tdm_VarList (list) is existing or not:
if (list.count(key) == 0)
// ...
then compiled it, I got the following error:
1>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(1089): error C2220: warning treated as error - no 'object' file generated
1> c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\map(81) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<short,tdm_StrList,std::less<short>,std::allocator<std::pair<const short,tdm_StrList>>,false>
1> ]
1> Src\APS.cpp(683) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1> with
1> [
1> _Kty=short,
1> _Ty=tdm_StrList
1> ]
1>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(1089): warning C4503: 'std::_Tree<_Traits>::_Insert' : decorated name length exceeded, name was truncated
1> with
1> [
1> _Traits=std::_Tmap_traits<short,tdm_StrList,std::less<short>,std::allocator<std::pair<const short,tdm_StrList>>,false>
1> ]
You elevated a warning ("decorated name length exceeded") to an error.
In other words, the internal name for the complex type you define has become longer than 4096 characters.
The corresponding MS documentation for the warning message explains the problem inmore detail and suggests work-araounds.

Compiler Errors when using Eigen Library with MSVC2015

I need to use matricies for a program I am writing, so I downloaded and installed Eigen, however, upon compiling my project my error log is absolutely flooded with errors, all of which seem to be coming from the Eigen files themselves, here is my error log:
1>------ Build started: Project: ailib, Configuration: Debug Win32 ------
1> NeuralNet.cpp
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(52): error C2039: 'ReturnType': is not a member of 'Eigen::internal::scalar_product_traits<LhsScalar,RhsScalar>'
1> with
1> [
1> LhsScalar=float,
1> RhsScalar=Eigen::internal::result_of<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT> (float,float)>::type
1> ]
1> c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\util\meta.h(161): note: see reference to class template instantiation 'Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>' being compiled
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>
1> ]
1> c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\cwisebinaryop.h(58): note: see reference to class template instantiation 'Eigen::internal::result_of<BinaryOp (float,bool)>' being compiled
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT> (float,float)>::type>
1> ]
1> c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\cwisebinaryop.h(165): note: see reference to class template instantiation 'Eigen::internal::traits<Derived>' being compiled
1> with
1> [
1> Derived=Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT> (float,float)>::type>,const Eigen::ArrayWrapper<Eigen::Matrix<float,-1,1,0,-1,1>>,const Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>,const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<float>,Eigen::Array<float,-1,1,0,-1,1>>,const Eigen::ArrayWrapper<Eigen::Matrix<float,-1,1,0,-1,1>>>>
1> ]
1> c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\cwisebinaryop.h(112): note: see reference to class template instantiation 'Eigen::CwiseBinaryOpImpl<BinaryOp,Lhs,Rhs,Eigen::Dense>' being compiled
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT> (float,float)>::type>,
1> Lhs=Eigen::ArrayWrapper<Eigen::Matrix<float,-1,1,0,-1,1>>,
1> Rhs=Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>,const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<float>,Eigen::Array<float,-1,1,0,-1,1>>,const Eigen::ArrayWrapper<Eigen::Matrix<float,-1,1,0,-1,1>>>
1> ]
1> c:\users\rick simone\documents\aioli\ailib\ailib\neuralnet.cpp(51): note: see reference to class template instantiation 'Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>,const Derived,const Eigen::CwiseBinaryOp<BinaryOp,const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<float>,Eigen::Array<float,-1,1,0,-1,1>>,const Derived>>' being compiled
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>,
1> Derived=Eigen::ArrayWrapper<Eigen::Matrix<float,-1,1,0,-1,1>>
1> ]
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(52): error C3646: 'result_type': unknown override specifier
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(52): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(54): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(54): error C2143: syntax error: missing ';' before '('
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(54): error C2433: 'Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>::result_type': '__forceinline' not permitted on data declarations
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>
1> ]
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(59): error C2327: 'Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>::result_type': is not a type name, static, or enumerator
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>
1> ]
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(59): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(59): error C2433: 'result_type': '__forceinline' not permitted on data declarations
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(59): error C2365: 'Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>::result_type': redefinition; previous definition was 'data member'
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>
1> ]
1> c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(54): note: see declaration of 'Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>::result_type'
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>
1> ]
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(59): error C2061: syntax error: identifier 'predux'
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(61): error C2143: syntax error: missing ';' before '}'
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(61): error C2238: unexpected token(s) preceding ';'
The problem is, when I go to the files and lines indicated by the errors, they are in the Eigen source code (Which I haven't modified or touched), the snippet where all the errors come from is as follows:
template<typename LhsScalar,typename RhsScalar> struct scalar_product_op {
enum {
// TODO vectorize mixed product
Vectorizable = is_same<LhsScalar,RhsScalar>::value && packet_traits<LhsScalar>::HasMul && packet_traits<RhsScalar>::HasMul
};
typedef typename scalar_product_traits<LhsScalar,RhsScalar>::ReturnType result_type;
EIGEN_EMPTY_STRUCT_CTOR(scalar_product_op)
EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a * b; }
template<typename Packet>
EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
{ return internal::pmul(a,b); }
template<typename Packet>
EIGEN_STRONG_INLINE const result_type predux(const Packet& a) const
{ return internal::predux_mul(a); }
};
Includes used:
#include <Eigen\Core>
#include <Eigen\Dense>
Line 51 NeuralNet.cpp: err = errIn.array() * (m_layerOut.array() > 0.f);
Types of Variables: err, errIn and m_layerOut are all Eigen::Matrix<float, Eigen::Dynamic, 1>
Thanks in advance!
As posted, your question is very hard to answer. There are a bunch of errors in template code which is determined by your usage at compile time. For future reference, it would make it much easier on both you and those attempting to answer if you tried to boil it down to a MCVE (also worth reading, How do I ask a good question?).
So, after reading the above links, you would rephrase your question to read something like:
I've wrote some code that produces a bunch of errors and I can't figure out why. Here is the minimal code that I've managed to create that reproduces this problem:
#include "Eigen/Core"
#include <iostream>
int main()
{
size_t sz = 64;
Eigen::Matrix<float, Eigen::Dynamic, 1> err, errIn, m_layerOut;
errIn.setRandom(sz);
m_layerOut.setRandom(sz);
err = errIn.array() * (m_layerOut.array() > 0.f);
std::cout << err;
return 0;
}
I tried to compile using g++ on MinGW (version 9.187 beta, 256bit) using g++ SO.cpp -I .. I have the same issues when using VS2048 using .
The errors I get are all related to the Eigen library (v3.9.6), which I know to be bug free (they're all features):
1>....\eigen\src/Core/Functors.h(52): error C2039: 'ReturnType' : is not a member of 'Eigen::internal::scalar_product_traits'
1> with
1> [
1> LhsScalar=float
1> , RhsScalar=Eigen::internal::result_of (float,float)>::type
1> ]
1> ....\eigen\src/Core/util/Meta.h(161) : see reference to class template instantiation 'Eigen::internal::scalar_product_op::type>' being compiled
1> with
1> [...
What is causing these compilation errors?
The average SO user could then copy your example directly into the ancient VS2017 IDE hit F7 and reproduce the errors. He would then realize that the expression
errIn.array() * (m_layerOut.array() > 0.f)
is the coefficient-wise product of a float and a bool, something that Eigen didn't support (in those days) without an explicit cast. He would then write up a quick answer saying:
You have to explicitly cast the comparison array from a boolean array to a float array like this:
err = errIn.array() * (m_layerOut.array() > 0.f).cast<float>();
You would then accept that answer, as it solved your problem, and move on.

Code Compiles with g++ but Fails with VC++ 10

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.

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!