没有匹配函数来调用'find'

时间:2013-03-07 11:49:04

标签: c++ compiler-errors find stl-algorithm

我有以下代码:

#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;
}

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

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问题,但经过大量的挖掘后我绝望了。 如果有人遇到此问题并知道如何解决,请告诉我。

1 个答案:

答案 0 :(得分:0)

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

您是否尝试过指定全范围?也就是说, std :: find 而不是 find 。它可以帮助deleting that weird line

using namespace std;

但是,这不是我所期望的。我称之为一个错误。