Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > map as member variable of a class

Reply
Thread Tools

map as member variable of a class

 
 
wang
Guest
Posts: n/a
 
      07-22-2010
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

 
Reply With Quote
 
 
 
 
haroon
Guest
Posts: n/a
 
      07-22-2010
On Jul 22, 4:55*pm, wang <(E-Mail Removed)> wrote:

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


Upgrade to a newer compiler / stop living in the past...
 
Reply With Quote
 
 
 
 
wang
Guest
Posts: n/a
 
      07-24-2010
On 23 Jul., 01:29, Christian Hackl <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
wang
Guest
Posts: n/a
 
      07-27-2010
On 24 Jul., 02:23, Stuart Golodetz
<(E-Mail Removed)> wrote:
> 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/2...tion-when-acce...
http://stackoverflow.com/questions/1...-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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments