[英]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所以,总结是
currentVertices
is an iterator (or pointer if you wanna say) and *currentVertices
is the content of that iterator
. currentVertices
是一个迭代器(或指针,如果你想说的话), *currentVertices
是该content of that iterator
。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 ¤tVertices : currentFace->setofVertices){
cout << currentVertices;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.