Help with map class and base class pointers

can anyone explain why I need to put record* in the map section? Also is this the best way of storing the base class pointers within the map? As I intend to create a database where I can search specific classes by using their id numbers (e.g ash=1, ru=2 , jim=3).

Also for my database search it returns the addresss of mappyiter->second. I was expecting to get the class jim. Ideally i would like to have some sort of mappyiter->print() (i.e jim->print()) but i cant get this to happen.... any ideas??

note// record is an abstract bass class, derived classses from this are physics, fashionm and civileng.

Thanks to anyone who wishes to give me some advice!


[code snippet removed, due to privacy concerns, 2013/04/08]


Last edited on by admin
You declare a map like map<key_type, value_type>. You want it to store record pointers, so value_type is record*. What else could you expect?

As for the method, it is fine, unless you know that all integers from is some range will each represent a record in which case a vector would be better.
Thank you very much!! Although I updated my problem since, any ideas for the next bit.
i would like to have some sort of mappyiter->print()
You can. You just have to make print virtual. (read about polymorphism)

Wait, if you call record an abstract class, you must know polymorphism.. So what exactly prevents you from using print?
Last edited on
I have tried to replace "mappyiter->second" by "mappyiter->print()" as i do have a virtual print function. However i get this compiling error

error C2039: 'print' : is not a member of 'std::pair<_Ty1,_Ty2>'
with
[
_Ty1=const int,
_Ty2=ashdatabase::record *
]

this confuses me as the print function is within all the classes.
Well, the thing that mappyiter points to is not a record*, but an std::pair that stores both the key and the value. So you have to use mappyiter->second->print().
I thought this and have tried that, sadly i get this error

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
Well, it seems to me that your code was cout << mappyiter->second->print(); where print is declared as virtual void print();. print returns a void and you try to feed that void to cout. I assume that print does the "cout << " thing itself, so just call it in another statement. If you really want to use it with <<, make it return a string.
ah yes, I was being very stupid. I shouldn't of been doing cout<< .....->print(); just print on its own. Thank you very much for your help!
Topic archived. No new replies allowed.