My program keeps crashing once I try to make my custom class use some of its own functions
My custom class looks like this:
Entity.h

#ifndef _ENTITIES
#define _ENTITIES
#include "actual.h"
#include "Manager.h"

struct Position
{
    float x, y, z;
};

class Entity
{

public:

    Entity();
    ~Entity();

    bool Init(Actual*, int);
    Actual* GetActual();
    Position* GetPosition();

    void SetPosition(float, float, float);
    void SetPosition(Position*);

    bool IsSpawned();

    bool Render(Manager*);
    bool Spawn(Position*);

    int GetID();

private:

    bool spawned;
    Actual* actual;
    Position* position;
    int entityID;

};

#endif

Entity.cpp

#include "stdafx.h"
#include "Entity.h"
#include <string>
#include "Debug.h"

Entity::Entity()
{
    spawned = false;
}

Entity::~Entity()
{

}

bool Entity::Init(Actual* base, int id)
{

    if (base->IsLoaded() == false)
    {
        return false;
    }

    std::string::size_type sz;

    entityID = id;
    actual = base;

    return true;

}

Position* Entity::GetPosition()
{
    return position;
}

void Entity::SetPosition(float x, float y, float z)
{
    Position* _P = new Position;
    _P->x = x;
    _P->y = y;
    _P->z = z;
    position = _P;
}

void Entity::SetPosition(Position* newPos)
{
    position = newPos;
}

bool Entity::IsSpawned()
{
    return spawned;
}

bool Entity::Spawn(Position* SpawnPos)
{

    pos = SpawnPos;
    spawned = true;

    return true;

}

int Entity::GetID() // this is the unique id that the entity gets
{
    return entityID;
}

// render the entity (only if spawned)
bool Entity::Render(OtherManager* rm)
{


    // this is where the crash happens
    if (IsSpawned() == true)
    {

        if (actual->HasEntryValue("EntityModel") == false)
            return false;

        model* m;

        rm->GetResource((char*)actual->GetEntryValue("EntityModel", 0).c_str(), m);

        // render code

        return true;

    }

    return false;

}

the Manager.h

#ifndef _MANAGER_
#define _MANAGER_

#include "Actual.h"
#include "Entity.h"
#include "OtherManager.h"
#include <Windows.h>

const int MAX_ENTITIES = 2024;

class Manager
{
public:
    Manager();
    ~Manager();

    bool AddComponent(Actual*, DifVector); // returns Entity

    bool Render(OtherManager*);

private:

    Entity* Entities[MAX_ENTITIES];

    int entity_count;
    int nextEID;

};

#endif

the Manager.cpp

#include "stdafx.h"
#include "Manager.h"
#include "Debug.h"

Manager::Manager()
{

    entity_count = -1;
    nextEID = 0;

}

Manager::~Manager()
{
}

bool Manager::AddComponent(Actual* base, DifVector spawnpos)
{

    bool result;

    entity_count = nextEID;

    Entities[entity_count] = new Entity();
    result = Entities[entity_count]->Init(base, entity_count);
    if (!result)
    {
        return false;
    }

    Position* tempPOS = new Position;

    tempPOS->x = spawnpos.x;
    tempPOS->y = spawnpos.y;
    tempPOS->z = spawnpos.z;

    result = Entities[entity_count]->Spawn(tempPOS);
    if (!result)
    {
        return false;
    }

    nextEID = nextEID + 1;

    return true;

}

bool Manager::Render(OtherManager* rm)
{

    if (entity_count = 0)
        Entities[0]->Render(rm);

    for (int i=0; i=nextEID; i++)
    {

        Entities[i]->Render(rm);

    }

    return true;

}

I have modified the code a bit to not shown all the stuff I dont think has anything to do with the problem.
The way I use the code is:

DifVector spawnposition = SelectionPositionSpawnFromCamera(MainCam);
_Manager->AddComponent(ToSpawn, spawnposition); // ToSpawn is an 'Actual' class that has been preset same goes for the spawnposition

// render function
_Manager->Render(rm_WB);

Build it with debugging symbols inserted, and then run it under a debugger. The debugger will show you the line of code it crashes on and allow you to examine the variables to see what the problem is.

Found the problem it was because my render loop accidentally rendered an unitialized class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.