| eibarra (10) | |||||||
So starting with the Item.h file :
I need to create a class to store the strings created by the Item.h file containing the id, description, and price. So up till now I created the following .h and .cpp file:
.CPP file:
I think I'm getting the wrong idea about strings. I thought I could add data just by using '+=' but apparently not . Please any help | |||||||
|
|
|||||||
| BigBlackSheep (17) | |
|
First of all, you need to include the <string> header. Secondly, you have a completely wrong idea of pointers, not strings. Read up on these: http://www.cplusplus.com/doc/tutorial/pointers/ http://www.cplusplus.com/doc/tutorial/dynamic/ In your case though, you should just use an std::vector class: http://www.cplusplus.com/reference/vector/vector/ | |
|
|
|
| MiiNiPaa (226) | |
1) I think you are mixing up string and vector. Strings exists to store textual data only. You cannot white some random data in it What you can do: (a) Use a vector<Item>; (b) overload string operator+=(string, Item) to do wht you want; (c) you can define casting Item → string and cast it before assigment.NOTE: Do not use (b) and (c). I have included them as a reference only! 2) string*_items += item;You are declaring a new variable _items, which shadows member variable with the same name. 3) string*_items += item;you are trying to add pointer to string and Item, which doesn't make any sense. | |
|
|
|
| eibarra (10) | |||||
|
Update to my code: .h File
.cpp File:
Seems like there's no need of pointers or vectors, works fine using only strings. Got a question though, when declaring the constructor how can I set a default value to the _items string? | |||||
|
Last edited on
|
|||||