错误:从'char'到'std :: vector <std :: basic_string <char>的用户定义转换无效

时间:2015-08-06 07:50:48

标签: c++ string c++11 vector

我在stl_algo.h中收到了上述错误,无法查明问题,因为涉及的媒介太多了。

    for (unsigned i=0;i < subsets.size(); i++) {
        do {
        permutations.push_back(subsets[i]);}
        while (next_permutation(begin(subsets[i]), end(subsets[i])));
    }


    for (unsigned i = 0; i < permutations.size(); i++) {
            if (find(wordlist.begin(), wordlist.end(), permutations[i]) != wordlist.end())
                first_words.push_back(permutations[i]);

    }

  ...

    for (const auto& str: subsets)
        set_difference(str.begin(), str.end(), letters.begin(), letters.end(), back_inserter(complement_sets));

1 个答案:

答案 0 :(得分:1)

您需要学习如何读取编译器的输出,通常它不仅会告诉您出现问题的行,还会告诉您行的起源位置,尤其是在使用模板(或宏)时。

所以,例如MSVC给出了以下输出:

c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm(3710): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'char' (or there is no acceptable conversion)
          c:\program files (x86)\microsoft visual studio 12.0\vc\include\iterator(59): could be 'std::back_insert_iterator<std::vector<std::string,std::allocator<_Ty>>> &std::back_insert_iterator<std::vector<_Ty,std::allocator<_Ty>>>::operator =(const std::back_insert_iterator<std::vector<_Ty,std::allocator<_Ty>>> &)'
          with
          [
              _Ty=std::string
          ]
          c:\program files (x86)\microsoft visual studio 12.0\vc\include\iterator(36): or       'std::back_insert_iterator<std::vector<std::string,std::allocator<_Ty>>> &std::back_insert_iterator<std::vector<_Ty,std::allocator<_Ty>>>::operator =(std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&)'
          with
          [
              _Ty=std::string
          ]
          c:\program files (x86)\microsoft visual studio 12.0\vc\include\iterator(30): or       'std::back_insert_iterator<std::vector<std::string,std::allocator<_Ty>>> &std::back_insert_iterator<std::vector<_Ty,std::allocator<_Ty>>>::operator =(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)'
          with
          [
              _Ty=std::string
          ]
          while trying to match the argument list '(std::back_insert_iterator<std::vector<std::string,std::allocator<_Ty>>>, char)'
          with
          [
              _Ty=std::string
          ]
          c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm(3748) : see reference to function template instantiation '_OutIt std::_Set_difference<_InIt1,_InIt2,_OutIt,_Pr>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt,_Pr)' being compiled
          with
          [
              _OutIt=std::back_insert_iterator<std::vector<std::string,std::allocator<std::string>>>
  ,            _InIt1=char *
  ,            _InIt2=char *
  ,            _Pr=std::less<void>
          ]
          c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm(3778) : see reference to function template instantiation '_OutIt std::_Set_difference2<char*,char*,_OutIt,_Pr>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt,_Pr,std::true_type)' being compiled
          with
          [
              _OutIt=std::back_insert_iterator<std::vector<std::string,std::allocator<std::string>>>
  ,            _Pr=std::less<void>
  ,            _InIt1=char *
  ,            _InIt2=char *
          ]
          c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm(3808) : see reference to function template instantiation '_OutIt std::set_difference<_InIt1,_InIt2,_OutIt,std::less<void>>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt,_Pr)' being compiled
          with
          [
              _OutIt=std::back_insert_iterator<std::vector<std::string,std::allocator<std::string>>>
  ,            _InIt1=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>
  ,            _InIt2=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>
  ,            _Pr=std::less<void>
          ]
          c:\users\marvin\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(43) : see reference to function template instantiation '_OutIt std::set_difference<std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,std::back_insert_iterator<std::vector<std::string,std::allocator<_Ty>>>>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt)' being compiled
          with
          [
              _OutIt=std::back_insert_iterator<std::vector<std::string,std::allocator<std::string>>>
  ,            _Ty=std::string
  ,            _InIt1=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>
  ,            _InIt2=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>
          ]

重要的是最后一行:

          c:\users\marvin\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(43) : see reference to function template instantiation '_OutIt std::set_difference<std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,std::_String_iterator<std::_String_val<std::_Simple_types<char>>>,std::back_insert_iterator<std::vector<std::string,std::allocator<_Ty>>>>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt)' being compiled
          with
          [
              _OutIt=std::back_insert_iterator<std::vector<std::string,std::allocator<std::string>>>
  ,            _Ty=std::string
  ,            _InIt1=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>
  ,            _InIt2=std::_String_iterator<std::_String_val<std::_Simple_types<char>>>
          ]

我的consoleapplication1.cpp第43行:

set_difference( str.begin(), str.end(), letters.begin(), letters.end(), back_inserter( complement_sets ) );

这里的问题是你要比较两组字符,但试图将它插入字符串列表中。 您需要将字符串传递给back_inserter而不是字符串向量。