S
subaruwrx88011
I posted this question earlier and made some changes. Due to the topic
getting off topic I decided to post another one.
Here is the code.
#include <map>
#include <string>
#include <iostream>
using namespace std;
class CCSI_EnviromentClass
{
private:
//Constructor
CCSI_EnviromentClass();
struct StateType
{
int m_integer;
string m_string;
float m_float;
StateType() : m_integer(-1), m_float(1.0) {}
};
typedef std::map<std::string, StateType> ItemStateMap;
ItemStateMap m_itemMap;
static CCSI_EnviromentClass *ms_instance;
public:
//Destructor
~CCSI_EnviromentClass();
//Methods
static CCSI_EnviromentClass *instance();
int getItemStateInt(std::string item);
float getItemStateFloat(std::string item);
std::string getItemStateString(std::string item);
int setItemState(std::string item, std::string state);
int setItemState(std::string item, float state);
int setItemState(std::string item, int state);
void print();
};
//Initialize
CCSI_EnviromentClass *CCSI_EnviromentClass::ms_instance = NULL;
CCSI_EnviromentClass::CCSI_EnviromentClass()
{
//cout << "Instance of EnviromentClass Created" << endl;
}
CCSI_EnviromentClass::~CCSI_EnviromentClass()
{
ms_instance = NULL;
//cout << "Instance of EnviromentClass Destroyed" << endl;
}
CCSI_EnviromentClass *CCSI_EnviromentClass::instance()
{
if( ms_instance == NULL)
{
ms_instance = new CCSI_EnviromentClass();
}
return ms_instance;
}
std::string CCSI_EnviromentClass::getItemStateString(std::string item)
{
StateType state;
ItemStateMap::iterator map_iterator;
//Find the item name in the list
map_iterator = m_itemMap.find(item);
//If the item was found then return the state, otherwise return NULL
if(map_iterator != m_itemMap.end())
{
state = m_itemMap[item];
return state.m_string; //return string part of union
}
else
{
return NULL;
}
}
float CCSI_EnviromentClass::getItemStateFloat(std::string item)
{
StateType state;
ItemStateMap::iterator map_iterator;
//Find the item name in the list
map_iterator = m_itemMap.find(item);
//If the item was found then return the state, otherwise return NULL
if(map_iterator != m_itemMap.end())
{
state = m_itemMap[item];
return state.m_float; //return float part of union
}
else
{
return -1;
}
}
int CCSI_EnviromentClass::getItemStateInt(std::string item)
{
StateType state;
ItemStateMap::iterator map_iterator;
//Find the item name in the list
map_iterator = m_itemMap.find(item);
//If the item was found then return the state, otherwise return NULL
if(map_iterator != m_itemMap.end())
{
state = m_itemMap[item];
return state.m_integer; //return float part of union
}
else
{
return -1;
}
}
int CCSI_EnviromentClass::setItemState(std::string p_item, std::string
p_state)
{
StateType state;
state.m_string = p_state;
m_itemMap.insert(make_pair(p_item,state)); //THIS IS WHERE IT IS SEG
FAULTING
//m_itemMap[p_item] = state; //THIS IS WHERE IT IS SEG FAULTING
return 0;
}
int CCSI_EnviromentClass::setItemState(std::string p_item, float
p_state)
{
StateType state;
state.m_float = p_state;
m_itemMap.insert(make_pair(p_item,state));
//m_itemMap[p_item] = state;
return 0;
}
int CCSI_EnviromentClass::setItemState(std::string p_item, int p_state)
{
StateType state;
state.m_integer = p_state;
m_itemMap.insert(make_pair(p_item,state));
//m_itemMap[p_item] = state;
return 0;
}
void CCSI_EnviromentClass:
rint()
{
ItemStateMap::iterator mapIterator;
for(mapIterator = m_itemMap.begin(); mapIterator != m_itemMap.end();
mapIterator++)
{
cout << mapIterator->first << " = ";
mapIterator->second.print();
cout << endl;
}
}
Can anyone tell me why this is seg faulting? That will greatly be
appreciated.
Thanks subaru88011
getting off topic I decided to post another one.
Here is the code.
#include <map>
#include <string>
#include <iostream>
using namespace std;
class CCSI_EnviromentClass
{
private:
//Constructor
CCSI_EnviromentClass();
struct StateType
{
int m_integer;
string m_string;
float m_float;
StateType() : m_integer(-1), m_float(1.0) {}
};
typedef std::map<std::string, StateType> ItemStateMap;
ItemStateMap m_itemMap;
static CCSI_EnviromentClass *ms_instance;
public:
//Destructor
~CCSI_EnviromentClass();
//Methods
static CCSI_EnviromentClass *instance();
int getItemStateInt(std::string item);
float getItemStateFloat(std::string item);
std::string getItemStateString(std::string item);
int setItemState(std::string item, std::string state);
int setItemState(std::string item, float state);
int setItemState(std::string item, int state);
void print();
};
//Initialize
CCSI_EnviromentClass *CCSI_EnviromentClass::ms_instance = NULL;
CCSI_EnviromentClass::CCSI_EnviromentClass()
{
//cout << "Instance of EnviromentClass Created" << endl;
}
CCSI_EnviromentClass::~CCSI_EnviromentClass()
{
ms_instance = NULL;
//cout << "Instance of EnviromentClass Destroyed" << endl;
}
CCSI_EnviromentClass *CCSI_EnviromentClass::instance()
{
if( ms_instance == NULL)
{
ms_instance = new CCSI_EnviromentClass();
}
return ms_instance;
}
std::string CCSI_EnviromentClass::getItemStateString(std::string item)
{
StateType state;
ItemStateMap::iterator map_iterator;
//Find the item name in the list
map_iterator = m_itemMap.find(item);
//If the item was found then return the state, otherwise return NULL
if(map_iterator != m_itemMap.end())
{
state = m_itemMap[item];
return state.m_string; //return string part of union
}
else
{
return NULL;
}
}
float CCSI_EnviromentClass::getItemStateFloat(std::string item)
{
StateType state;
ItemStateMap::iterator map_iterator;
//Find the item name in the list
map_iterator = m_itemMap.find(item);
//If the item was found then return the state, otherwise return NULL
if(map_iterator != m_itemMap.end())
{
state = m_itemMap[item];
return state.m_float; //return float part of union
}
else
{
return -1;
}
}
int CCSI_EnviromentClass::getItemStateInt(std::string item)
{
StateType state;
ItemStateMap::iterator map_iterator;
//Find the item name in the list
map_iterator = m_itemMap.find(item);
//If the item was found then return the state, otherwise return NULL
if(map_iterator != m_itemMap.end())
{
state = m_itemMap[item];
return state.m_integer; //return float part of union
}
else
{
return -1;
}
}
int CCSI_EnviromentClass::setItemState(std::string p_item, std::string
p_state)
{
StateType state;
state.m_string = p_state;
m_itemMap.insert(make_pair(p_item,state)); //THIS IS WHERE IT IS SEG
FAULTING
//m_itemMap[p_item] = state; //THIS IS WHERE IT IS SEG FAULTING
return 0;
}
int CCSI_EnviromentClass::setItemState(std::string p_item, float
p_state)
{
StateType state;
state.m_float = p_state;
m_itemMap.insert(make_pair(p_item,state));
//m_itemMap[p_item] = state;
return 0;
}
int CCSI_EnviromentClass::setItemState(std::string p_item, int p_state)
{
StateType state;
state.m_integer = p_state;
m_itemMap.insert(make_pair(p_item,state));
//m_itemMap[p_item] = state;
return 0;
}
void CCSI_EnviromentClass:
{
ItemStateMap::iterator mapIterator;
for(mapIterator = m_itemMap.begin(); mapIterator != m_itemMap.end();
mapIterator++)
{
cout << mapIterator->first << " = ";
mapIterator->second.print();
cout << endl;
}
}
Can anyone tell me why this is seg faulting? That will greatly be
appreciated.
Thanks subaru88011