вектор равен для типаработает в g ​​++, но не в visual studio 2010 - PullRequest
0 голосов
/ 31 января 2012
#include<iostream>
#include<vector>
std::vector<std::string> vector1;
int main() {
    vector1.push_back("adadad");
    std::vector<std::string> vector2;
    vector2.push_back("adadd");
    if (vector1==vector2) {
        std::cout<<"success";
    } else {
       vector1.swap(vector2);
       vector2.clear();
       vector2.push_back("adadd");
       if (vector1==vector2) {
           std::cout<<"success_swap";
       }
    }
}

Теперь это работает в g ​​++, но не в visual studio. Оператор == здесь не работает и выдает ошибку компиляции в Visual Studio 2010 (окончательная версия). То же самое работает, если вектор целочисленного типа . Я что-то здесь упустил? Это не родная вещь Linux, которую они пропустили. Почему есть реализация в gcc, а не в vc ++?

Отображаемое сообщение об ошибке:

[snip]\vc\include\xutility(2990): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
          ]
          [snip]\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
          [snip]\vc\include\exception(475): or       'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
          [snip]\vc\include\exception(481): or       'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
          [snip]\vc\include\system_error(408): or       'bool std::operator ==(const std::error_code &,const std::error_condition &)'
          [snip]\vc\include\system_error(416): or       'bool std::operator ==(const std::error_condition &,const std::error_code &)'
          while trying to match the argument list '(const std::basic_string<_Elem,_Traits,_Ax>, const std::basic_string<_Elem,_Traits,_Ax>)'
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
          ]
          [snip]\vc\include\xutility(3030) : see reference to function template instantiation 'bool std::_Equal<_InIt1,_InIt2>(_InIt1,_InIt1,_InIt2)' being compiled
          with
          [
              _InIt1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> *,
              _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>
          ]
          [snip]\vc\include\xutility(3051) : see reference to function template instantiation 'bool std::_Equal1<const std::basic_string<_Elem,_Traits,_Ax>*,_InIt2>(_InIt1,_InIt1,_InIt2,std::tr1::true_type)' being compiled
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>,
              _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
              _InIt1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> *
          ]
          [snip]\vc\include\vector(1489) : see reference to function template instantiation 'bool std::equal<std::_Vector_const_iterator<_Myvec>,std::_Vector_const_iterator<_Myvec>>(_InIt1,_InIt1,_InIt2)' being compiled
          with
          [
              _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>,
              _InIt1=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
              _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>
          ]
          [snip]\test\main.cpp(8) : see reference to function template instantiation 'bool std::operator ==<std::string,std::allocator<_Ty>>(const std::vector<_Ty> &,const std::vector<_Ty> &)' being compiled
          with
          [
              _Ty=std::string
          ]

Ответы [ 2 ]

1 голос
/ 31 января 2012

Ошибка возникает из-за того, что в MSVC оператор равенства std::string (==) не включен в <iostream> или <vector>.Вы также должны включить <string>.

Ключевая строка в сообщении:;"ошибка C2678: двоичный файл" == ": не найден оператор, который принимает левый операнд типа 'const std :: basic_string <_Elem, _Traits, _Ax>' (или нет приемлемого преобразования)"

0 голосов
/ 31 января 2012

Вы, вероятно, используете действительно старый компилятор.

Это работает для меня в MSVS 2005, которая сама по себе не нова.

Если ваш компилятор C++03 совместим, он должен работать:

23.2.4

template <class T, class Allocator>
bool operator == ( const vector<T,Allocator>& x,
                   const vector<T,Allocator>& y);
...