简体   繁体   中英

std::find on std::vector< std::string > does not compile in Visual C++ 2008?

I tried this code on Visual C++ 2008 express edition, but it does not compile:

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    typedef std::string Element;
    typedef std::vector< Element > Vector;
    typedef Vector::iterator Iterator;

    Vector v;
    std::find( v.begin(), v.end(), std::string( "xxx" ) );

    return 0;
}

I get the following error:

c:\programmi\microsoft visual studio 9.0\vc\include\algorithm(40) : error C2784: 'bool  std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'std::basic_string<_Elem,_Traits,_Ax>'

The same code is corrected compiled by gcc and works as expected.

Is it a bug of Visual Studio? And how can I get my example working on Visual C++ 2008?

You forgot to #include <string> .

You must always include all the headers that you need for your code. Never depend on magic recursive inclusions that happen to work sometimes. For everything you use in your code you must know where it has been declared and guarantee that the declarations are visible in your translation unit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM