让我感到困惑的是,即使这个cpp文件包含包含友人模板类声明和定义的头文件,我也需要在声明模板的特定实例之前进行前向声明。 非常感谢。
//BagPtr.h file
template <typename T>
class BagPtr{
public:
typedef typename std::vector<T>::size_type size_type;
BagPtr():_weak_ptr(make_shared<std::vector<T>>()){}
//need to be friend of tempalte class Bag
BagPtr(const class Bag<T>& b, size_type i = 0):_weak_ptr(b._ptr), index(i){}
private:
weak_ptr<std::vector<T> > _weak_ptr;
size_type index;
};
//other.h
//includes BagPtr.h
//forward declaration.
//Compiler complains that Bag is not a class template if there is no forward declaration.
template <typename T> class BagPtr;
template <typename T>
class Bag{
public:
friend class BagPtr<T>;
//other defination
};