Reputation: 2207
I've looked at a few threads on stackoverflow before I thought of asking this question (since there has been a couple out there), but even through implementing some of the solutions, it doesn't seem to help my problem. That or I'm doing it wrong.
Anyways, as it goes, the error I'm getting is:
error: no match for 'operator<<' in 'os << itr'
my class is:
template <typename T>
class btree {
public:
btree(size_t maxNodeElems);
~btree() {}
struct node { // <- this is just a declaration of a private inner-class
list <T> elements;
node *lvl;
};
private:
size_t maxNodeElems;
node* root; // <- this is the actual private member
};
template <typename T>
btree<T>::btree(size_t maxNodeElems) {
if (maxNodeElems > 0) maxNodeElems = maxNodeElems;
root = new node;
root->lvl = new node[maxNodeElems+1];
}
template <typename T>
pair <typename btree<T>::iterator, bool> btree <T>::insert (const T& elem) {
root->elements.push_back(elem);
root->elements.sort();
std::pair <typename btree<T>::iterator, bool> e;
return e;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const btree<T>& tree) {
class list <T>::iterator itr = tree.root->elements.begin();
for (; itr != tree.root->elements.end(); ++itr) os << itr;
return os;
}
I'm quite aware that my insert returns nothing, but I haven't finished implementing that function, I'm just trying to test how to get the elements out of the list at the moment. Can someone tell me what I'm doing wrong?
Upvotes: 2
Views: 1370
Reputation: 361802
Use *itr
instead of itr
:
os << *itr;
This is the desired behaviour.
Now, for this to work, you have to make sure that operator<<
exists for type T
which is basically the type of *itr
.
Instead of manual loop, you could use std::copy
as well:
template <typename T>
std::ostream& operator<<(std::ostream& os, const btree<T>& tree) {
std::copy(tree.root->elements.begin(),
tree.root->elements.end(),
std::ostream_iterator<T>(os, " "));//print " " between 2 elements
return os;
}
Upvotes: 3