I have a templated class that I am trying to print out using operator << but get the errors:
Vertex.h:24:16: error: use of deleted function 'std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)'
where line 24 refers to the return of the output operator in the following code:
/In declaration of templated class Vertex
friend std::ostream operator<<(std::ostream& ost, const Vertex<T>& v) {
ost << v.getItem();
return ost;
}
//In main function
Vertex<int>* v1 = new Vertex<int>();
v1->setItem(15);
cout << *v1 << endl;
How can I get this output working?
std::ostream
and siblings doesn't have copy constructors and copy assignment operators[1], and when you made the following possibly typoed code
std::ostream operator<<(std::ostream& ost, const Vertex<T>& v) {
// ^ Returning by value
ost << v.getItem();
return ost;
}
you are actually trying to return a copy of ost
. To fix that, you must return the stream by reference.
std::ostream& operator<<(std::ostream& ost, const Vertex<T>& v) {
// ^ This
[1] In C++11, they are actually marked as =delete
d
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With