仅仅定义从模板类派生的类是不够的
Defining class derived from template class is not enough
"A.h">
template<class T>
struct A
{
void foo();
};
template<class T>
struct B : A<T> {};
"A.cpp">
template<class T>
void A<T>::foo()
{
}
template struct B<int>;
当我在其他源文件中调用B的方法foo时,由于未解析的外部符号,它不会构建。为什么A不能用B来定义?至少我觉得这很奇怪。有没有办法从B的定义中强制a的定义?我尝试在结构B中添加"usingA::foo;",但它没有改变任何内容。
我知道我可以简单地定义A,但我不想这样做,因为在我的真实代码中,"A"包含的模板参数比"B"更多,我担心我会犯错误,直到有人编写需要这些未解决方法的代码时才会被检测到。我也很好奇为什么它不起作用。
在.h文件中定义A::foo。
A.h
template<class T>
struct A
{
void foo();
};
template<class T>
struct B : A<T> {};
template<class T>
void A<T>::foo()
{
}
A.cpp
template struct B<int>;
相关文章:
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 在派生函数中指定void*参数
- 如何通过派生类函数更改基类中的向量
- 如何委托派生类使用其父构造函数?
- 如何使用单独文件中的派生类访问友元函数对象
- 派生类销毁的最佳实践是什么
- 如何使用基类指针引用派生类成员
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何引用基类的派生类?
- 存储模板类型以强制转换回派生<T>
- 需要从 istream 和 ostream 派生 iostream
- 在 C++ 中用派生类型重写成员函数
- 具有多个类、派生类的C++正向声明
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 如果基类包含双指针成员,则派生类的构造函数
- 为什么此派生对象无法访问基类的后递减方法?
- 仅仅定义从模板类派生的类是不够的