当我构建以下源代码时:
#include <stdio.h>
class Heap
{
public:
Heap()
{
printf("Heap()");
}
void* print()
{
printf("This is Heap Print");
}
};
class DvHeap : public Heap
{
public:
DvHeap():Heap()
{
printf("DvHeap()\n");
};
};
template<class T>
class base
{
public:
void* operator new(size_t size)
{
printf("base()\n");
return T::printit().print();
}
};
template<class T>
class derived : public base<derived<T> >
{
static DvHeap xx;
public:
static Heap& printit()
{
printf("DvHeap()\n");
return xx;
}
};
int main()
{
//DvHeap *pH = new DvHeap(1);
derived<DvHeap> *pD = new derived<DvHeap>;
return 0;
}
我收到以下错误:
[debdghos]$ g++ Ctest.cpp -o 测试
/tmp/ccM7XI3u.o: 在函数derived<DvHeap>::printit()':
Ctest.cpp:(.text._ZN7derivedI6DvHeapE7printitEv[derived<DvHeap>::printit()]+0xf): undefined reference to
中派生::xx' collect2: ld 返回 1 退出状态
谁能告诉我为什么会这样?该代码用于学习目的。
谢谢