如果我了解您要执行的操作,您将缺少 list_template 的全局前向声明:
我的类.h
// forward declarator. must match definition in list_template.h
template<typename T> class list_template;
class MyClass
{
public:
MyClass() {};
virtual ~MyClass() {};
template<typename T> friend class list_template;
template<typename T> friend ostream& operator <<(ostream&, const list_template<T>&);
};
list_template.h
template<typename T>
class list_template
{
public:
list_template() {};
virtual ~list_template() {};
// declare friend ostream operator <<
friend ostream& operator << <>(ostream& os, const list_template<T>& lt);
};
// ostream insertion operator <<
template<typename T>
ostream& operator <<(ostream& os, const list_template<T>& lt)
{
// TODO: use lt here.
return os;
}
至少我认为这离你要去的地方很近。