Reputation: 13
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>’)
Upvotes: 1
Views: 8921
Reputation: 117168
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(auto& currentVertices : currentFace->setofVertices) {
cout << currentVertices; // already dereferenced
}
Upvotes: 1
Reputation: 11340
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. Either dereference the iterator
// V
cout << *currentVertices;
or simplify the whole loop:
for(const auto ¤tVertices : currentFace->setofVertices){
cout << currentVertices;
}
Upvotes: 0
Reputation: 594
currentVertices
is an iterator here. It's an object that acts as a pointer. You cannot print it with 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
. (Read as 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
.cout
the content of iterator
not the iterator
Upvotes: 1
Reputation: 38267
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
cout << currentVertices;
tries to invoke operator <<
with a std::ostream
reference (the std::cout
) and an iterator into a 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).
Upvotes: 1