Beginner in C++ need help

T

Tan Tze Yong

Dear all,

I am tasked to write a program in C++ recently. However my knowledge in
C++ is very limited. What happened is I am storing serveral vectors in a
map. I need to retrieve the vector from the map and insert/remove items.
I can figured out how to use a map and vector but I tried encapsulate
the map in a class, I got problem retrieving the vector from it. The
vector refused to store the items I add. It must be a silly mistake on
my part. I have uploaded the source code to my server at
http://www.our-moments.com/orderbook.zip I hope some kind soul can help
me figured what went wrong. Your help will be greatly appreciated.
 
R

Rolf Magnus

Tan said:
Dear all,

I am tasked to write a program in C++ recently. However my knowledge in
C++ is very limited. What happened is I am storing serveral vectors in a
map. I need to retrieve the vector from the map and insert/remove items.
I can figured out how to use a map and vector but I tried encapsulate
the map in a class, I got problem retrieving the vector from it.

What do you mean by "retrieving"?
The
vector refused to store the items I add. It must be a silly mistake on
my part.

Maybe you add the item to a copy instead of the vector in the map?
I have uploaded the source code to my server at
http://www.our-moments.com/orderbook.zip I hope some kind soul can help
me figured what went wrong. Your help will be greatly appreciated.

Ok, I now had a look, and I wonder why you're using so many pointers and
dynamically allocated objects. And where do you delete all those objects?
Anyway, the problem you're referring to seems to be here:

bool OrderBooks::findOrderBook(string aSymbol, OrderBook *aOrderBook)
{
map<string, OrderBook*>::iterator itr;

itr = m_OrderBooks.find(aSymbol);

if (itr != m_OrderBooks.end())
{
aOrderBook = itr->second;

aOrderBook (i.e the pointer) is passed to the function by value. That means
that the function operates on a local copy of it. Only that copy is
modified in your function, so the above assignment doesn't have any effect.

return true;
}
else
return false;
}
 
M

Mike Wahler

aOrderBook (i.e the pointer) is passed to the function by value. That
means
that the function operates on a local copy of it. Only that copy is
modified in your function, so the above assignment doesn't have any
effect.

return true;
}
else
return false;
}

To Tan Tze Yong:

Now that Rolf has identified the problem for you, find your
solution by reading about 'pass by reference'.

-Mike
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top