简体   繁体   English

'operator&lt;&lt;' 不匹配(操作数类型为 'std::ostream' {aka 'std::basic_ostream<char> '} 和 'const std::type_index')</char>

[英]No match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const std::type_index’)

Actually, I'm trying to use the visitor pattern with some templates.实际上,我正在尝试将访问者模式与一些模板一起使用。

I want to parse my unordered_map wich contains type_index and the function variable but I get a compilation error that I don't understand even after reading a lot of topics about it.我想解析我的unordered_map包含type_indexfunction变量,但我得到一个编译错误,即使在阅读了很多关于它的主题后我也不明白。

error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const std::type_index’)
       std::cout << i.first << i.second << std::endl;

Here is my loop who doesn't compile这是我没有编译的循环

for (auto i : functions) {
      std::cout << i.first << i.second << std::endl;
}

Here is my code snippet with my loop to parse and display what is inside the unordered_map这是我的代码片段和我的循环来解析和显示unordered_map里面的内容

template <typename TReturn> struct AnyVisitor {
  using typeInfoRef = std::reference_wrapper<const std::type_info>;
  using function = std::function<TReturn(std::any &)>;
  std::unordered_map<std::type_index, function> functions;

  template <typename TArg> void accept(std::function<TReturn(TArg &)> f) {
    functions.insert(std::make_pair(std::type_index(typeid(TArg)),
                                    function([&f](std::any &x) -> TReturn {
                                      return f(std::any_cast<TArg &>(x));
                                    })));

    for (auto i : functions) {
      std::cout << i.first << i.second << std::endl;
    }
  }

  TReturn operator()(std::any &x) {
    try {
      auto function = functions.at(std::type_index(x.type()));

      return function(x);
    } catch (...) {
      throw std::runtime_error("No visitor registered");
    }
  }
};

If anyone has an idea of how to resolve it, I'll gladly take it !如果有人知道如何解决它,我很乐意接受! Thanks谢谢

You should try to print i.first.name() instead of i.first only您应该尝试打印i.first.name()而不是i.first only

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:“operator&lt;&lt;”不匹配(操作数类型为“std::ostream {aka std::basic_ostream”<char> }&#39; 和 &#39;void&#39;) - error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’) 'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream {aka std::basic_ostream<char> }' 和 '分数')</char> - No match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Fraction') 与&#39;operator &lt;&lt;&#39;不匹配(操作数类型为&#39;std :: ostream {aka std :: basic_ostream <char> }” - no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream<char> '}</char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream {aka std::basic_ostream<char> }'和'列表')</char> - error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'List') 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream<char> '} 和 'std::_List_iterator<int> ')</int></char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::_List_iterator<int>’) 错误:“operator&lt;&lt;”不匹配(操作数类型为“std::basic_ostream”<char> &#39; - error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ 如何修复错误:'operator&lt;&lt;' 不匹配(操作数类型为 'std::ostream {aka std::basic_ostream<char> }' 和 'void')同时使用字符串和堆栈</char> - How to fix error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void') while using strings and stack \\ main.cpp | 103 |错误:“ operator &lt;&lt;”不匹配(操作数类型为“ std :: ostream {aka std :: basic_ostream <char> }”和“人”) - \main.cpp|103|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Person') 运算符重载时出错(错误:“运算符&lt;&lt;”不匹配(操作数类型为“std::basic_ostream”)<char> &#39; 和 &#39;const 字符 [2]&#39;) - Error while operator overloading (error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘const char [2]’)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM