在C+中调用虚函数时出现分段错误+; 我目前正在为我的游戏引擎使用C++的ECS。我有一个基本系统结构,它有两个虚拟函数init()和update(),它们在派生结构中实现。我有一个使用模板的addSystem()和removeSystem()函数,我有一个System*系统数组。如果我试着给他们打电话,这会给我一个分段错误

在C+中调用虚函数时出现分段错误+; 我目前正在为我的游戏引擎使用C++的ECS。我有一个基本系统结构,它有两个虚拟函数init()和update(),它们在派生结构中实现。我有一个使用模板的addSystem()和removeSystem()函数,我有一个System*系统数组。如果我试着给他们打电话,这会给我一个分段错误,c++,segmentation-fault,entity-component-system,C++,Segmentation Fault,Entity Component System,系统: struct System{ public: uint32_t id; virtual void init(World* world){} virtual void update(World* world){} }; addSystem(): 模板 void addSystem(){ T*系统=分配(); 系统->id=getID(); 系统。附加(系统); #ifdef调试 日志(“ECS:系统添加成功”); #endif//DEBUG } removeSy

系统:

struct System{
public:
    uint32_t id;
    virtual void init(World* world){}
    virtual void update(World* world){}
};
addSystem():

模板
void addSystem(){
T*系统=分配();
系统->id=getID();
系统。附加(系统);
#ifdef调试
日志(“ECS:系统添加成功”);
#endif//DEBUG
}
removeSystem():

模板
无效删除系统(uint32\u t id){
无符号整数索引;
对于(int i=0;iid==id){
指数=i;
打破
}
}
系统。删除(索引);
}
从系统*调用虚拟函数:

for (int i = 0; i < systems.size; i++){
    systems[i]->init(this); // Here is the segmentation fault.
}

for (int i = 0; i < systems.size; i++){
    systems[i]->update(this); // Here is the segmentation fault.
}
for(int i=0;iinit(this);//这是分段错误。
}
对于(int i=0;iupdate(this);//这是分段错误。
}
请询问是否需要更多信息

编辑:
size
在for循环中等于1,systems[i]是有效指针。我还测试了
p systems[I]->update
,它也有一个有效的地址。问题在于调用它时。

\ifndef SYSTEMTEST\u H_
#ifndef SYSTEMTEST_H_
#define SYSTEMTEST_H_

#include <stdint.h> 
#include <vector>
#include <iostream>

struct World
{
    int id;
};

struct System{
public:
    uint32_t id;
    virtual void init(World* world){}
    virtual void update(World* world){}
};

int systemID = 0;
std::vector<System*> systems;


struct Derived : System
{
    void init(World* world){
        std::cout << "init" << std::endl;
    }
    void update(World* world){
        std::cout << "update" << std::endl;
    }
};

uint32_t getID()
{
    return systemID++;
}

template<typename T> void addSystem(){
    T* system = new T();
    system->id = getID();
    //systems.append(system);
    systems.push_back(system);
}

template<typename T> void removeSystem(uint32_t id){
    unsigned int index;
    for (int i = 0; i < systems.size; i++){
        if (systems[i]->id == id){
            index = i;
            break;
        }
    }
    //remove operator
    //systems.remove(index);
}
#endif
#定义系统测试_ #包括 #包括 #包括 结构世界 { int-id; }; 结构系统{ 公众: uint32_t id; 虚拟void init(世界*世界){} 虚拟空间更新(世界*世界){} }; int systemID=0; 向量系统; 结构派生:系统 { void init(世界*世界){
std::当您直接调用时,您是否可以共享错误代码?这不应该发生。虚拟函数的要点是不必“记住”此ptr来自哪种类型并让VTABLE执行任务您是否尝试过使用
valgrind
或gcc/clang的地址消毒剂?两者都会提供更多信息。@StephenNewell我使用了代码块调试器,当我键入
p system[I]->update(此)
它说
无法访问地址0x8处的内存
。如果没有工作示例(我可以运行),我无法进行调查。但您主要需要解决segfault问题,而不是寻找转换为原始类型的方法。如果您能提供一个
最小可复制示例
,我可能能够提供更多帮助。此外,我假设系统是一个标准容器,在这种情况下,您需要调用size()而不是sizeth。该错误听起来像
系统[I]
nullptr
。你认为
size
正确吗?它对我也适用。但你能告诉我你做了什么使它工作。系统无法实例化,因为存在虚拟函数。你必须继承系统和实现方法。我仍然不明白,这就是我正在做的。你想做什么做
for (int i = 0; i < systems.size; i++){
    systems[i]->init(this); // Here is the segmentation fault.
}

for (int i = 0; i < systems.size; i++){
    systems[i]->update(this); // Here is the segmentation fault.
}
#ifndef SYSTEMTEST_H_
#define SYSTEMTEST_H_

#include <stdint.h> 
#include <vector>
#include <iostream>

struct World
{
    int id;
};

struct System{
public:
    uint32_t id;
    virtual void init(World* world){}
    virtual void update(World* world){}
};

int systemID = 0;
std::vector<System*> systems;


struct Derived : System
{
    void init(World* world){
        std::cout << "init" << std::endl;
    }
    void update(World* world){
        std::cout << "update" << std::endl;
    }
};

uint32_t getID()
{
    return systemID++;
}

template<typename T> void addSystem(){
    T* system = new T();
    system->id = getID();
    //systems.append(system);
    systems.push_back(system);
}

template<typename T> void removeSystem(uint32_t id){
    unsigned int index;
    for (int i = 0; i < systems.size; i++){
        if (systems[i]->id == id){
            index = i;
            break;
        }
    }
    //remove operator
    //systems.remove(index);
}
#endif
#include <iostream>
#include "SystemTest.h"
using namespace std;

int main(int argc, char** argv){

    addSystem<Derived>();
    for (int i = 0; i < systems.size(); i++)
    {
        World *world;
        world = new World;
        systems[i]->init(world);
    }
    return 0;

}