I have a weird problem. I have a vector that I would like to push objects on to like so:
vector<DEMData>* dems = new vector<DEMData>();
DEMData* demData = new DEMData();
// Build DEMDATA
dems->push_back(*demData);
There will be a few hundred DEMData objects in the vector. The problem is when this code is finished, all items are equal to the last item "pushed back" to the vector?
Why are the other objects being overridden in the vector?
edit:
The DemData class is simple, just a data structure with setters and getters:
class DEMData
{
private:
int bitFldPos;
int bytFldPos;
const char* byteOrder;
const char* desS;
const char* engUnit;
const char* oTag;
const char* valType;
int index;
public:
void SetIndex(int index);
int GetIndex() const;
void SetValType(const char* valType);
const char* GetValType() const;
void SetOTag(const char* oTag);
const char* GetOTag() const;
void SetEngUnit(const char* engUnit);
const char* GetEngUnit() const;
void SetDesS(const char* desS);
const char* GetDesS() const;
void SetByteOrder(const char* byteOrder);
const char* GetByteOrder() const;
void SetBytFldPos(int bytFldPos);
int GetBytFldPos() const;
void SetBitFldPos(int bitFldPos);
int GetBitFldPos() const;
friend std::ostream &operator<<(std::ostream &stream, DEMData d);
};
EDIT:
I am reading an XML file and building DEMData objects based on the attributes of each xml element:
DEMData demData;
for (i = 0; attr[i]; i += 2)
{
if(strcmp(attr[i],"BitFldPos") == 0)
{
demData.SetBitFldPos(*attr[i + 1] - '0');
}
else if(strcmp(attr[i],"BytFldPos") == 0)
{
char* pEnd;
int tmp = strtol(attr[i + 1],&pEnd,10);
demData.SetBytFldPos(tmp);
}
else if(strcmp(attr[i],"ByteOrder") == 0)
{
demData.SetByteOrder(attr[i + 1]);
}
else if(strcmp(attr[i],"DesS") == 0)
{
demData.SetDesS(attr[i + 1]);
}
else if(strcmp(attr[i],"EngUnit") == 0)
{
demData.SetEngUnit(attr[i + 1]);
}
else if(strcmp(attr[i],"OTag") == 0)
{
demData.SetOTag(attr[i + 1]);
}
else if(strcmp(attr[i],"ValTyp") == 0)
{
demData.SetValType(attr[i + 1]);
}
else if(strcmp(attr[i],"idx") == 0)
{
char* pEnd;
int tmp = strtol(attr[i + 1],&pEnd,10);
demData.SetIndex(tmp);
}
//printf(" %s='%s'", attr[i], attr[i + 1]);
}
// Insert the data in the vector.
dems.push_back(demData);