簡體   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’)

實際上,我正在嘗試將訪問者模式與一些模板一起使用。

我想解析我的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;

這是我沒有編譯的循環

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

這是我的代碼片段和我的循環來解析和顯示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");
    }
  }
};

如果有人知道如何解決它,我很樂意接受! 謝謝

您應該嘗試打印i.first.name()而不是i.first only

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM