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. 静态成员变量的定义:

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

相关推荐
gxn_mmf2 天前
典籍知识问答重新生成和消息修改Bug修改
前端·bug
千里马学框架3 天前
重学安卓14/15自由窗口freeform企业实战bug-学员作业
android·framework·bug·systrace·安卓framework开发·安卓窗口系统·自由窗口
Bear on Toilet4 天前
Bug日记——实现“日期类”
开发语言·c++·bug
Direction_Wind4 天前
flinksql bug : Max aggregate function does not support type: CHAR
bug
mg6684 天前
Python Bug 修复案例分析:多线程数据竞争引发的bug 两种修复方法
bug
黎猫大侠5 天前
一次Android Fragment内存泄露的bug解决记录|Fragment not attach to an Activity
android·bug
七七小报5 天前
uniapp-商城-48-后台 分类数据添加修改弹窗bug
uni-app·bug
windwind20005 天前
发行基础:本地化BUG导致审核失败
游戏·青少年编程·编辑器·bug·创业创新·玩游戏
Htht1115 天前
【Qt】之【Bug】点击按钮(ui->pushButton)触发非本类设置的槽函数
qt·ui·bug