Confusion with std::map

Discussion in 'C++' started by Bob, Aug 31, 2004.

  1. Bob

    Bob Guest

    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.
     
    Bob, Aug 31, 2004
    #1
    1. Advertising

  2. Bob

    David Hilsee Guest

    "Bob" <> wrote in message
    news:m45Zc.224$...
    > 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
     
    David Hilsee, Sep 1, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Hildebrand
    Replies:
    5
    Views:
    8,104
    krogers
    Mar 20, 2012
  2. Peter Jansson
    Replies:
    5
    Views:
    6,388
    Ivan Vecerina
    Mar 17, 2005
  3. Replies:
    1
    Views:
    440
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,037
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    2,045
    James Kanze
    Dec 21, 2008
Loading...

Share This Page