When I try to run my program( which is a test entity system for anyone who's wondering ), I get a Stack Overflow error, and I cannot find the source of this error.
Here is my code:
baseentity.h:
#ifndef BASEENTITY_H
#define BASEENTITY_H
#include <vector>
#define DECLARE_CLASS( className ) public: virtual char const *_GetClassName( ) { return #className; }
class BaseEntity{
DECLARE_CLASS( BaseEntity )
private:
typedef enum{
eTouch,
eCollide,
eSpawn
} m_eEvents;
int m_nID;
static int sm_nCount;
public:
static std::vector<BaseEntity> sm_vEntities;
BaseEntity( );
~BaseEntity( );
virtual void OnTouch( );
virtual void OnCollide( );
virtual void OnSpawn( );
virtual void FireEvent( int nEvent );
};
#endif //BASEENTITY_H
baseentity.cpp:
#include <vector>
#include <iostream>
#include "baseentity.h"
std::vector<BaseEntity> BaseEntity::sm_vEntities = std::vector<BaseEntity>(100000);
int BaseEntity::sm_nCount = 1;
BaseEntity::BaseEntity( )
:m_nID( sm_nCount )
{
sm_vEntities.push_back( *this );
sm_nCount++;
}
BaseEntity::~BaseEntity( )
{
sm_vEntities.erase( sm_vEntities.begin( ) + m_nID );
}
void BaseEntity::FireEvent(int nEvent)
{
switch( nEvent )
{
case eSpawn:
OnSpawn( );
case eCollide:
OnCollide( );
case eTouch:
OnTouch( );
}
}
void BaseEntity::OnCollide( )
{
}
void BaseEntity::OnTouch( )
{
std::cout << _GetClassName( ) << "::OnTouch( ): This class does not have a OnTouch function.\n";
}
void BaseEntity::OnSpawn( )
{
std::cout << _GetClassName( ) << "::OnSpawn( ): This class does not have a OnSpawn function.\n";
}
main.cpp:
#include <iostream>
#include <string>
#include "baseentity.h"
//not my function
void split_string(const std::string& src, std::vector<std::string>& dst, char split)
{
int startPos = 0, endPos = src.find(split);
while(endPos != std::string::npos)
{
dst.push_back(src.substr(startPos, endPos-startPos));
startPos = endPos + 1;
endPos = src.find(split, startPos);
}
}
int main( )
{
std::string strCommand;
std::vector<std::string> vInputs;
while( strCommand != "exit" )
{
std::cout << "> ";
getline( std::cin, strCommand );
split_string( strCommand, vInputs, ' ' );
if( vInputs[0] == "spawn" )
{
for( int i = 0; i < BaseEntity::sm_vEntities.size( ); ++i )
{
if( BaseEntity::sm_vEntities[i]._GetClassName( ) == vInputs[1] )
{
BaseEntity eTemp;
eTemp.OnSpawn( );
}
}
}
if( vInputs[0] == "touch" )
{
for( int i = 0; i < BaseEntity::sm_vEntities.size( ); ++i )
{
if( BaseEntity::sm_vEntities[i]._GetClassName( ) == vInputs[1] )
{
BaseEntity eTemp;
eTemp.OnTouch( );
}
}
}
if( vInputs[0] == "collide" )
{
for( int i = 0; i < BaseEntity::sm_vEntities.size( ); ++i )
{
if( BaseEntity::sm_vEntities[i]._GetClassName( ) == vInputs[1] )
{
BaseEntity eTemp;
eTemp.OnCollide( );
}
}
}
}
std::cin.ignore( 1200, '\n' );
std::cin.get( );
}
Thank you for your time.