undefined reference to CPUAllocatorSingleton::instance

它发生的原因是你声明了 CPUAllocatorSingleton 类中的 instance 变量,但没有提供它的定义。

这个错误是链接器无法找到 CPUAllocatorSingleton::instance 的定义。它发生的原因是你声明了 CPUAllocatorSingleton 类中的 instance 变量,但没有提供它的定义。

具体来说,错误信息显示:

  • undefined reference to CPUAllocatorSingleton::instance :编译器找不到 CPUAllocatorSingleton::instance 的定义。

  • **relocation against _ZN21CPUAllocatorSingleton8instanceE'**:这表明链接器正在试图将代码链接到 CPUAllocatorSingleton::instance`,但是找不到该变量的实际位置。

解决方法:

  1. 定义 instance 变量:

    你在 CPUAllocatorSingleton 类中声明了 instance 变量,但它需要在 .cpp 文件中进行定义。可以像下面这样定义 instance

    cpp 复制代码
    // 在头文件中声明
    class CPUAllocatorSingleton {
    public:
        static std::shared_ptr<CPUAllocator> getInstance();
    
    private:
        static std::shared_ptr<CPUAllocator> instance;  // 声明静态成员变量
    };
    
    // 在 .cpp 文件中定义
    std::shared_ptr<CPUAllocator> CPUAllocatorSingleton::instance = nullptr;  // 定义静态成员变量
  2. 静态成员变量的定义:

    静态成员变量需要在类外部进行定义即使它已经在类内声明了。 否则,编译器就找不到该变量的地址,从而导致链接错误。

相关推荐
l1t2 天前
修改一个触发PostgreSQL 17.2 bug的SQL
sql·postgresql·bug
包小黑2 天前
【Linux】bug登记好习惯:发现bug,用命令行截取对应日志
linux·bug
癫狂的兔子4 天前
【BUG】【Python】逆序取值为空
bug
癫狂的兔子4 天前
【BUG】【Python】精确度问题
python·bug
癫狂的兔子4 天前
【BUG】【Python】合并两个列表
bug
癫狂的兔子4 天前
【BUG】【Python】eval()报错
python·bug
余生H4 天前
Ai编程翻车修车记3 -一次因为移除监听器失败导致bug后的DOM事件深入学习
学习·bug·ai编程
癫狂的兔子4 天前
【BUG】【Python】list切片和list.reverse()的区别
bug
gladiator+5 天前
Weblog项目bug合集
bug
workflower6 天前
小强地狱(Bug Hell)
大数据·bug·团队开发·需求分析·个人开发·结对编程