gpt4 book ai didi

c++ - 编译时错误 "collect2: ld returned 1 exit status"

转载 作者:行者123 更新时间:2023-11-28 07:19:32 24 4
gpt4 key购买 nike

当我构建以下源代码时:

#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 个退出状态

谁能告诉我为什么会这样?该代码用于学习目的。

谢谢

最佳答案

您应该在类之外初始化静态成员。

template<typename T>
DvHeap derived<T>::xx;

关于c++ - 编译时错误 "collect2: ld returned 1 exit status",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19705886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com