I am trying to write some code to calculate dividend yields. I need to calculate the dividend yield (dividend/stock_price) daily. Dividends are constant. Eventually, I will have to tie the stock price to a dynamic feed, but in the meantime the stock prices may be in their own map. I inserted the dividend amounts for each stock in the class constructor because it is less computationally cumbersome than using conditional statements in the members (i.e if stock = Apple, then dividend is X). I am getting the following error message starting at the ‘[‘ before the “AAPL”:
**error C2679: binary '[' : no operator found which takes a right-hand operand of type 'const
char [5]' (or there is no acceptable conversion) c:\boost/unordered/unordered_map.hpp(415):
could be 'double &boost::unordered_map<K,T>::operator [](const FinModels::Instrument *const &)'
with
[
K=const FinModels::Instrument *,
T=double
]
while trying to match the argument list '(DividendMap, const char [5])'**
Can anyone help me based on my brief code below and description? Is const Symbol* incorrect type for the key?
Also, if it’s bad convention to post the fixed map values in the constructor, please let me know what is better.
Header File
public:
typedef boost::unordered_map<const Symbol*, double> Dividend_Map;
typedef Dividend_Map::iterator Dividend_MapIterator;
private:
Dividend_Map p_dividend_map;
.CPP File
p_dividend_map["AAPL"] = 0.01;
p_dividend_map["BAC"] = 0.01;
p_dividend_map["C"] = 0.01;
The key type for your map is a
Symbol *, and you’re trying to stick a bunch ofconst char *into the map. This will not work unlessSymbolistypedef‘d aschar.Create a
Symbolobject out of each symbol you want to store in the map and then add the address of that to the map.In the example above, the lifetime of
aaplmust match its lifetime as a member of the map. If the lifetime ofaaplexpires you’ll have the map pointing to an invalid memory location.You may want to change the map to
Then use it as