是否可以在基类中声明一个并将实现保留在派生类中?
我认为这是可能的,但以下情况不起作用。
#include <vector>
template <typename I>
class A {
public:
A() {}
template <typename T2, typename A2>
void test0(const std::vector<T2,A2>& vec,const I n) {
test(vec[0]);
}
private:
template <typename I2> void test(const I2 a);
};
template <typename I>
class B: public A<I> {
public:
B() {}
private:
template <typename I2> void test(const I2 n) {}
};
int main() {
B<int> b;
std::vector<int> x(100);
b.test0(x,0);
}
链接错误:
/tmp/test7-76b4cc.o: In function `void A<int>::test0<int, std::allocator<int> >(std::vector<int, std::allocator<int> > const&, int)':
test7.cpp:(.text._ZN1AIiE5test0IiSaIiEEEvRKSt6vectorIT_T0_Ei[_ZN1AIiE5test0IiSaIiEEEvRKSt6vectorIT_T0_Ei]+0x35): undefined reference to `void A<int>::test<int>(int)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)