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::s tring, 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(co nst std::string& s, const T& t)
{
tListOf.push_ba ck(t);
tMapOf[s] = &(tListOf.back( ));
}
I then use a lookup function to get data, like so:
//--------------------------------------------------------------------------
-
template <class T>
const int cGenericMeasure mentList<T>::Co lIDFromName(con st 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. 1 1962
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Woodster |
last post by:
I have declared the following
std::map<std::string, std::string> myMap;
to pass myMap to functions should I be declaring functions as:
void function(std::map<std::string, std::string>);
or is there a preferred/better method of doing this?
|
by: Antti Granqvist |
last post by:
Hello!
I have following object relations:
Competition 1--* Category 1--* Course
1
|
|
*
Course
|
by: Serengeti |
last post by:
Hello,
in my class I have a map that translates strings to pointers to some
member functions. The code goes like this:
class F {
typedef void (Function::*MathFuncPtr)();
std::map<std::string, MathFuncPtr> predefinedFunctions;
// lots of other stuff
void makeDictionary(){ predefinedFunctions=&F::f_sin(); }
};
|
by: Saeed Amrollahi |
last post by:
Dear All C++ Programmers
Hello
I am Saeed Amrollahi. I am a software engineer in
Tehran Sewerage Company. I try to use std::map and map::find member
function. I
use Visual Studio .NET. my program uses two MFC classes: CRect and
CPoint
which represents Rectangle and Point concepts (as usual) and a user
|
by: Erik Wikström |
last post by:
First of all, forgive me if this is the wrong place to ask this question,
if it's a stupid question (it's my second week with C++), or if this is
answered some place else (I've searched but not found anything).
Here's the problem, I have two sets of files, the name of a file contains a
number which is unique for each set but it's possible (even probable) that
two files in different sets have the same numbers. I want to store these...
| |
by: Avery Fong |
last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug
This program is developed under Visual Studio .NET 2003 in a Win32 Console Project
// VectorInsert.cpp : Defines the entry point for the console application
/
#include "stdafx.h
#include "VectorInsert.h
#ifdef _DEBU
|
by: kamaraj80 |
last post by:
Hi
I am using the std:: map as following.
typedef struct _SeatRowCols
{
long nSeatRow;
unsigned char ucSeatLetter;
}SeatRowCols;
typedef struct _NetData
|
by: digz |
last post by:
Hi,
I am trying to write a program which has two threads one of them write
to a map , and the other one deletes entries based on a certain
criterion..
first I cannot get the delete portion to work , what am i missing
here.
also is it possible/correct that the removeKeyValue function acquire
the mutex lock only during the call to map.erase(), and not the during
the whole iteration process as i have done here( i logically felt it
was...
|
by: mveygman |
last post by:
Hi,
I am writing code that is using std::map and having a bit of an issue
with its performance.
It appears that the std::map is significantly slower searching for an
element then a sequential search in a vector.
Has anyone run into this before?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |