Let
Foo.hpp :
class Foo
{
public:
void print() const;
protected:
vector<Bar<string, int>*> bar_;
};
void Foo::print() const
{
copy(bar_.begin(), bar_.end(), ostream_iterator<Bar<string, int>*>(cout, "\n"));
}
Bar.hpp :
class Bar
{
public:
template <typename K, typename U>
friend ostream& operator<<(ostream&, const Bar<K, U>&);
}
template <typename Key, typename T>
ostream& operator<<(ostream& out, const Bar<Key, T>& bar)
{
return out << "FOOBAR";
}
Where bar_ is an attribute of Foo and is a vector of pointers to Bar elements. Assume bar_ has one element, then the output of :
Foo foo;
foo.print();
is the address of the element in bar_, instead of "FOOBAR". If I wouldn't use pointers, the output would be "FOOBAR" What I'm trying to achieve here is to print "FOOBAR" for each pointer of bar_.
Try adding:
template <typename Key, typename T>
ostream& operator<<(ostream& out, Bar<Key, T>* bar)
{
return out << *bar;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.