编译时出现错误"collect2:ld 返回 1 个退出状态"
Error "collect2: ld returned 1 exit status" while compiling
当我构建以下源代码时:
#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 test
/tmp/ccM7XI3u.o:在函数中 derived<DvHeap>::printit()':
Ctest.cpp:(.text._ZN7derivedI6DvHeapE7printitEv[derived<DvHeap>::printit()]+0xf): undefined reference to
派生::xx'收集2:LD 返回 1 个退出状态
谁能告诉我为什么会这样?该代码用于学习目的。
谢谢
您应该在类外部初始化静态成员。
template<typename T>
DvHeap derived<T>::xx;
完整代码:
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;
}
};
template<typename T>
DvHeap derived<T>::xx;
int main()
{
//DvHeap *pH = new DvHeap(1);
derived<DvHeap> *pD = new derived<DvHeap>;
return 0;
}
在你错的地方阅读以下内容:http://www.learncpp.com/cpp-tutorial/811-static-member-variables/
相关文章:
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 为什么系统函数总是在C++中返回已转移的退出状态?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 试图在崇高中奔跑. 错误 已发生: 收集2.exe: 错误: ld 返回 1 退出状态
- collect2:错误:ld 返回 1 个退出状态未定义的引用
- C++ collect2:错误:ld 返回 1 退出状态
- 找不到 -llibmysqld Collect2.exe:错误:ld 返回 1 退出状态
- 在 c++ 中从执行的 shell 命令获取返回状态的安全方法是什么?
- 每次我要使用外部资源时,我都会得到:collect2。exe:错误 ld 返回 1 退出状态
- 返回状态后,功能不会结束
- "Extern"对象问题:错误:Id 返回 1 个退出状态
- c++ (C::B) 错误:ld 返回 1 个退出状态
- 测试用例状态为失败,但返回的值为 true
- C++ grpc::experimental:interceptor 如何从自定义拦截器返回状态和消息
- Boost Statechart:返回以前的状态
- 智能卡获取响应返回状态 6D00
- Easyhook:非托管挂钩,如何调用原始函数/更改返回状态
- 如何在单独的线程中调用方法并使用返回状态
- 为什么 std::生成类似于 std::for_each 的返回状态?