Confusion with std::map

B

Bob

Hi,

I'm trying to use a map with a string key, and a pointer to objects
contained in a vector. I've wrapped this in a class like so:

// cMap
template<class T> class cMap : public cList<T> { //
private:
protected:
std::map<std::string, T*> tMapOf; // map container
public:
void Add(const std::string&, const T& t); // add new object to map
void Clear();
const std::size_t Count() const; // number of list items

cMap(); // default constructor
cMap(const cMap&); // copy constructor
cMap& operator=(const cMap&); // assignment operator
virtual ~cMap(); // destructor
};

cList is a similar wrapper around a vector, defined as so:

// cList definition
template<class T> class cList { // base class for Lists
private:
protected:
std::vector<T> tListOf; // field list container
public:
void Add(const T& t); // add new object to list
void Clear();
void Reserve(const std::size_t&);
const std::size_t Count() const; // number of list items

cList(); // default constructor
cList(const cList&); // copy constructor
cList& operator=(const cList&); // assignment operator
virtual ~cList(); // destructor

T& operator[](int pos); // subscript operator
const T& operator[](int pos) const; // subscript operator
};

cMap::Add is implemented like:

template<class T>
void cMap<T>::Add(const std::string& s, const T& t)
{
tListOf.push_back(t);
tMapOf = &(tListOf.back());
}


I then use a lookup function to get data, like so:

//--------------------------------------------------------------------------
-
template <class T>
const int cGenericMeasurementList<T>::ColIDFromName(const string& s)
{
if(tMapOf.find(s) != tMapOf.end())
return tMapOf->ColID();
else return -1;
}


My problem is that this code works fine with Borland C++ Compiler (the one
with BCB 5 Pro) but when I compile and run with g++ (3.3 I think) the data
return by ColIDFromName is garbage.

If I replace the pointers with copies of the objects, this works fine with
either compiler and the data returned is correct, but then I have two sets
of identical data.

I'm sure I'm doing something totally stupid, but can't see what. Can anyone
advise what is wrong with my code (probably lots, as I'm new!!)???

Many thanks for your time,
Steve.
 
D

David Hilsee

Bob said:
Hi,

I'm trying to use a map with a string key, and a pointer to objects
contained in a vector. I've wrapped this in a class like so:
<snip>

The most likely problem is that the std::vector is reallocating its storage,
which causes the pointers to be invalidated. Either use indexes instead of
the pointers, or use a container that won't invalidate your pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top