map as member variable of a class

W

wang

Hi all,
I have the class declaration as follows:

class MyClass {
public:
map<string, string> account;
}

The main program uses the class as follows:

MyClass customer;
string acc = "abc", ort = "xyz";
customer.account[acc] = ort;

In VC++6, the compilation succeeded, but debug causes the error when
running the last instruction ("customer.account[acc] = ort;"):

Unhandled exception in customer.exe (MSVCRTD.DLL): 0xC0000005: Access
Violation.

What should be done to avoid this? Many thanks in advance!

kwwang
 
W

wang

There is nothing wrong with your code. If you really need VC6 to get it
right then you should probably ask in a Microsoft newsgroup.

Thank you, Christian! I've written a simple console test program,
which runs really well. In my program which throws the exception, the
class is defined in a .DLL. In the console test program, all are in
one file. Maybe this is the reason.
kwwang
 
W

wang

I seem to remember encountering this problem in the past. Here are a few
relevant links to things which look like it:

http://support.microsoft.com/kb/172396 http://support.microsoft.com/kb/168958/
http://stackoverflow.com/questions/2451714/access-violation-when-acce...
http://stackoverflow.com/questions/1085873/dll-memory-manager-mixup

Off the top of my head, try using the multithreaded DLL versions of the
runtime library for both your .dll and your .exe.

Thank you, Stuart! On the page of the first link you indicated above,
I found already a solution: accessor method instead of direct
subscipting.

Now another question: besides accessing the container via a key (which
can
be easily realized by accessor method), I'd like
to iterate through the whole container, and the container might be
very
complicated, i.e. a component of the container is itself a container,
e.g. nested container as map< string, map< string, vector
<class_object> > > .
How can such operation (iteration) be realized by means of an accessor
method?

kwwang
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top