我有一个使用模板并可以存储多种抽象数据类型 (ADT) 的“存储库”(仓库,随便你怎么称呼)。
代表
template <typename TAD>
class Repository {
public:
DynamicArray <TAD *> tad; // made a dynamic array myself, also uses templates
// since one ADT has one of the following two functions and the other doesn't
// I decided to not use TAD here
Person *findByName (string name);
Activities* findByDate(string date);
void save (TAD &p);
//etc
}
Rep.cpp
template <>
void Repository<Person>::save(Person &p) throw (RepositoryException) {
@code
}
template <>
void Repository<Activities>::save(Activities& a) throw (RepositoryException) {
@code
}
//etc
现在我有一个单独处理 ADT 的 Controller ,所以我想创建一个仅反射(reflect)抽象数据类型“Person”的存储库
我如何调用?(创建一个以 Person 或 Activity 作为模板...参数的存储库类型的对象?)
像这样: ? (下)
PersonController.h
Repository<Person> *repository;
ActivityController.h
Repository<Activities> *repository;
您无法链接到模板,因为链接器无法创建特化——编译器需要这样做。您需要将您的模板(来自 Rep.cpp)放入您的 Rep.h 文件中,以便编译器可以创建您需要的特化。
我是一名优秀的程序员,十分优秀!