By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Confusion with std::map

P: n/a
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[s] = &(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[s]->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.
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Bob" <no***@nowhere.com> wrote in message
news:m4***************@newsfe5-gui.ntli.net...
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.

--
David Hilsee
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.