简体   繁体   English

错误:'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>’)

Hello im trying to print a list of ints and i keep getting that erro.您好,我正在尝试打印整数列表,但我不断收到该错误。

I have a structure that have a list on it.我有一个结构,上面有一个列表。

struct faceFiguration{

    int faceID;
    list<int> setofVertices;

};

And i have a list of that sructure我有那个结构的清单

 list<faceFiguration> pattern;

and heres where i get confuse, i trie to print the lists here:这是我感到困惑的地方,我尝试在这里打印列表:

void PrintFaces(){

      currentFace = pattern.begin();
      while(currentFace != pattern.end()){

        cout << currentFace -> faceID << endl;

        for(auto currentVertices = currentFace->setofVertices.begin(); currentVertices != currentFace->setofVertices.end(); currentVertices++){

          cout << currentVertices;

        }

        cout << '\n';
        currentFace++;
      }

    }

This is the full message error这是完整的消息错误

error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'std::__cxx11::list<int>')

I don't think the error message really belongs to the line that causes the problem here (have you tried << -ing the list itself before?), but我不认为错误消息真的属于导致问题的行(您之前是否尝试过<< -ing list 本身?),但是

cout << currentVertices;

tries to invoke operator << with a std::ostream reference (the std::cout ) and an iterator into a std::list .尝试使用std::ostream引用( std::cout )和迭代器调用operator <<std::list That doesn't work, because the iterator type doesn't have this operator (why should it).那是行不通的,因为迭代器类型没有这个运算符(为什么要这样)。 However, iterators are modeled after pointers, and hence allow for dereferencing to access the element they are referring to.但是,迭代器是在指针之后建模的,因此允许取消引用以访问它们所引用的元素。 Long story short;长话短说; this should work:这应该工作:

cout << *currentVertices;

where the * in front of currentVertices is the dereferencing and yields an int& (reference to the underlying list element).其中currentVertices前面的*是取消引用并产生一个int& (对基础列表元素的引用)。

currentVertices is an iterator here. currentVertices是一个迭代器。 It's an object that acts as a pointer.它是一个用作指针的 object。 You cannot print it with cout .你不能用cout打印它。 But yes, you can print out the value that the iterator is pointing at.但是,是的,您可以打印出迭代器指向的值。 To do so you have to put a * before the iterator.为此,您必须在迭代器之前放一个* That is, *currentVertices .也就是说, *currentVertices (Read as content of currentVertices ) (读作content of currentVertices

So, the summary is所以,总结是

  1. currentVertices is an iterator (or pointer if you wanna say) and *currentVertices is the content of that iterator . currentVertices是一个迭代器(或指针,如果你想说的话), *currentVertices是该content of that iterator
  2. You need to cout the content of iterator not the iterator您需要计算cout器的content of iterator不是iterator

You've already gotten answers telling you to dereference the iterator:您已经得到了告诉您取消引用迭代器的答案:

for(auto currentVertices = currentFace->setofVertices.begin();
    currentVertices != currentFace->setofVertices.end();
    currentVertices++)
{
    cout << *currentVertices;   // dereference
}

You could however do that automatically by using a range-based for loop:但是,您可以使用基于范围的 for 循环自动执行此操作:

for(auto& currentVertices : currentFace->setofVertices) {
    cout << currentVertices;    // already dereferenced
}

As others already mentioned正如其他人已经提到的

 cout << currentVertices;

attempts to print an iterator.尝试打印迭代器。 But there is no overload for operator<< that takes a second parameter of this type.但是对于接受此类型的第二个参数的operator<<没有重载。 Either dereference the iterator要么取消引用迭代器

 //      V
 cout << *currentVertices;

or simplify the whole loop:或简化整个循环:

for(const auto &currentVertices : currentFace->setofVertices){
    cout << currentVertices;
}

暂无
暂无

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

相关问题 错误:'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> }&#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> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} 错误:'operator<<' 不匹配(操作数类型为'std::ostream {aka std::basic_ostream<char> }' 和 'std::ostream {aka std::basic_ostream<char> }')</char></char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::ostream {aka std::basic_ostream<char>}’) '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::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') '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’)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM