【问题标题】:error: invalid user-defined conversion from ‘char’ to ‘std::vector<std::basic_string<char>错误:从‘char’到‘std::vector<std::basic_string<char>的用户定义转换无效
【发布时间】:2015-10-29 06:05:40
【问题描述】:

我在 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));

【问题讨论】:

  • 在哪一行出现此错误?
  • auto 默认按取东西。所以你只是对字符串的副本等进行排序。auto&amp; 是你想要的,它将使用对原始对象的引用。
  • 它出现在主调试器之外,在 stl_algo.h 第 6000 行,在 __glibcxx_function_requires(_OutputIteratorConcept<_outputiterator>
  • @aj1204 您想通过引用访问元素的任何地方。 const auto&amp; 如果您不打算修改它们。
  • @aj1204 编译器通常会打印导致错误的语法分析的整个“堆栈”。里面应该有一些你的代码。

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


【解决方案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,而不是字符串向量。

【讨论】:

    猜你喜欢
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多