4

我有以下代码:

#include <algorithm>
#include <vector>
#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{   
    typedef vector<int> IntContainer;
    typedef IntContainer::iterator IntIterator;

    IntContainer vw;

    IntIterator i = find(vw.begin(), vw.end(), 5);

    if (i != vw.end()) 
        {
        printf("Find 5 in vector\n");   // found it
      }
        else 
        {
            printf("Couldn't find 5 in vector\n");  // couldn't found it
        }

    return 0;
}

我尝试在 Ubuntu 上使用 gcc 4.7.1 编译它并得到以下错误:

vec_test.cpp: In function ‘int main()’:
vec_test.cpp:27:46: error: no matching function for call to ‘find(std::vector<int>::iterator, std::vector<int>::iterator, int)’
vec_test.cpp:27:46: note: candidate is:
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/locale_facets.h:50:0,
                 from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/basic_ios.h:39,
                 from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/ios:45,
                 from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/ostream:40,
                 from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/iostream:40,
                 from vec_test.cpp:3:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/streambuf_iterator.h:371:5: note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> > >::__type std::find(std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, const _CharT2&)
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/streambuf_iterator.h:371:5: note:   template argument deduction/substitution failed:
vec_test.cpp:27:46: note:   ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’ is not derived from ‘std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >’

这段代码没有做任何事情,因为向量没有用任何内容初始化,但它应该编译。

我怀疑这是一个 gcc 问题,但经过大量挖掘后,我绝望了。如果有人遇到此问题并知道如何解决,请告诉我。

4

1 回答 1

0

也许某些文件在全局命名空间中定义了一个函数find ?

您是否尝试过指定全范围?也就是说,std::find而不是find。它可以帮助删除那条奇怪的行

using namespace std;

然而,这不是我所期望的。我会称之为错误。

于 2013-08-21T13:36:41.530 回答