模板类中的静态方法

static method in template class

本文关键字:静态方法      更新时间:2023-10-16

我有一个像下面这样的class

template<class T>
class Student
{
 public:
  static Student& Instance();
}
template<class T>
Student<T>& Student<T>::Instance()
{
 ...
 return student;
}

返回LNK2001错误。我可以这样用吗?

你的代码模棱两可,所以我试着猜测你的问题。如果您在Student.h中声明类Student并在Student.cpp中实现方法Instance(),那就是您的问题。在Student.h.

中实现方法Instance()

要么Student<T>::Instance应该在头文件中,要么你应该在.cpp文件中提供Student<>方法的专门化