让
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";
}
其中bar_是Foo的属性,是指向Bar元素的指针的向量。假设bar_有一个元素,然后输出:
Foo foo;
foo.print();
是bar_中元素的地址,而不是&#34; FOOBAR&#34;。如果我不使用指针,输出将是&#34; FOOBAR&#34;我在这里想要实现的是打印&#34; FOOBAR&#34;对于bar _的每个指针。
答案 0 :(得分:1)
尝试添加:
template <typename Key, typename T>
ostream& operator<<(ostream& out, Bar<Key, T>* bar)
{
return out << *bar;
}