I am trying to run an old C++ code in Linux (Redhat). I am using gcc version 4.1.2.
here is the code sample where i am getting error :
template <class TP> TP *GCVVector<TP>::Find(const TP &Obj)
{
#ifdef WIN32
using namespace std;
typedef typename vector<TP>::iterator Viterator;
#else
#ifdef __HP_aCC
using namespace std;
typedef typename vector<TP>::iterator Viterator;
#else
using namespace std;
typedef typename std::vector<TP>::iterator Viterator;
#endif
#endif
Viterator pCurrent =NULL ;
The error i am getting is this :
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVVector.h: In member function âTP* GCVVector<TP>::Find(const TP&) [with TP = GCVAsso<GCVString, GCVString>::KeyNode]â:
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVAsso.h:165: instantiated from âbool GCVAsso<KTP, VTP>::Add(KTP, VTP) [with KTP = GCVString, VTP = GCVString]â
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.h:69: instantiated from here
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVVector.h:398: error: conversion from âlong intâ to non-scalar type â__gnu_cxx::__normal_iterator<GCVAsso<GCVString, GCVString>::KeyNode*, std::vector<GCVAsso<GCVString, GCVString>::KeyNode, std::allocator<GCVAsso<GCVString, GCVString>::KeyNode> > >â requested
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVAsso.h:165: instantiated from âbool GCVAsso<KTP, VTP>::Add(KTP, VTP) [with KTP = GCVString, VTP = GCVString]â
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.h:69: instantiated from here
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVVector.h:403: error: no match for âoperator=â in âpCurrent = GCVVector<TP>::BinarySearch [with TP = GCVAsso<GCVString, GCVString>::KeyNode](0l, (GCVVector<TP>::GetSize [with TP = GCVAsso<GCVString, GCVString>::KeyNode]() - 1l), ((const GCVAsso<GCVString, GCVString>::KeyNode&)((const GCVAsso<GCVString, GCVString>::KeyNode*)Obj)))â
/usr/lib/gcc/x86_64-redhat- linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_iterator.h:634: note: candidates are: __gnu_cxx::__normal_iterator<GCVAsso<GCVString, GCVString>::KeyNode*, std::vector<GCVAsso<GCVString, GCVString>::KeyNode, std::allocator<GCVAsso<GCVString, GCVString>::KeyNode> > >& __gnu_cxx::__normal_iterator<GCVAsso<GCVString, GCVString>::KeyNode*, std::vector<GCVAsso<GCVString, GCVString>::KeyNode, std::allocator<GCVAsso<GCVString, GCVString>::KeyNode> > >::operator=(const __gnu_cxx::__normal_iterator<GCVAsso<GCVString, GCVString>::KeyNode*, std::vector<GCVAsso<GCVString, GCVString>::KeyNode, std::allocator<GCVAsso<GCVString, GCVString>::KeyNode> > >&)
make[2]: *** [CMakeFiles/GCVCore.dir/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.o] Error 1
make[1]: *** [CMakeFiles/GCVCore.dir/all] Error 2
The original code was written against an STL where
std::vector<T>::iteratorwas a raw pointer and so could be (and needed to be) initialised to NULL.For full compatibility, change the line to
In C++11, you can use
By full compatibility it is meant here that
Viteratormay be simply a bare pointer. In such a case, explicitly setting it to a default-constructed value will set it to NULL. Below is a simple example to demonstrate it.The output is:
Note that the program may still be incorrect if it does anything with
pCurrent, other than assigning it a new value (it would be valid to compare it against itself, or another iterator initialised by copying it, but comparing against a non-singular iterator, or a separately default value-constructed iterator would be undefined).