2

我正在尝试在 Linux (Redhat) 中运行旧的 C++ 代码。我正在使用 gcc 版本 4.1.2。这是我收到错误的代码示例:

           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 ;

我得到的错误是这样的:

         /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
4

2 回答 2

3

原始代码是针对std::vector<T>::iterator原始指针的 STL 编写的,因此可以(并且需要)初始化为 NULL。

为了完全兼容,将行更改为

Viterator pCurrent = Viterator();

在 C++11 中,您可以使用

Viterator pCurrent{};

完全兼容在这里意味着它Viterator可能只是一个裸指针。在这种情况下,将其显式设置为默认构造值会将其设置为 NULL。下面是一个简单的例子来演示它。

#include <iostream>

typedef void * Iterator;

int main(int, char**)
{
  Iterator v1, v2=Iterator();
  std::cout << "uninitialized pointer: " << v1 << "\ninitialized pointer: " << v2 << std::endl;
}

输出是:

uninitialized pointer: 0x7fff5fc01052
initialized pointer: 0

请注意,如果程序对 进行任何操作pCurrent,除了给它分配一个新值之外,它可能仍然是不正确的(将它与自身或通过复制它初始化的另一个迭代器进行比较是有效的,但与非奇异迭代器进行比较,或者一个单独的默认值构造的迭代器将是未定义的)。

于 2012-06-22T10:33:49.017 回答
0

迭代器不是数字,它是大多数时候不将数字作为构造函数参数的对象。

只需将行替换为

Viterator pCurrent = Viterator();

它将以与当前代码相同的方式出现错误。

于 2012-06-22T10:17:53.947 回答