Question

I'm a c++ noob trying to build a personal finance websocket server that tracks all of my assets and liabilities in real-time.

I've found that I can make maps of maps to have a multi-dimensional system of key-value pairs.

I've also found that boost::any and boost::variant can be used to store multiple types for a values. My problem is that some levels aren't very complex compared to others. For example, a bank account will only have a value, the amount in the account, while a brokerage account will have many types of investments and characteristics, so I'd like to do something like (in json):

{
    'bankAccount': 100.00,
    'brokerageAccount': {
        'stocks': {
            'companyName': 'Stack Exchange',
            'ticker': 'STAK',
            'pps': bazillion
            ...

Where bankAccount and brokerageAccount can be inserted and erased when needed and discarded as necessary.

I don't really know where to go from here. When I try to put

map<string, boost::any> accounts;
accounts["cash"] = 100;
accounts["brokerageAccount"] = map<string, boost::any>;

in the private section of broadcast_server in this websocket server, gcc with these flags -I ~/websocketpp-master/ -std=c++0x -D_WEBSOCKETPP_CPP11_STL_ -D_WEBSOCKETPP_NO_CPP11_REGEX_ -lboost_regex -lboost_system -L/usr/lib -pthread -O0 -ljson_spirit gives error: ‘accounts’ does not name a type for the last two lines.

How best can I store data in a json type way above, with the ability to add and delete keys and values anywhere?

Was it helpful?

Solution

accounts["brokerageAccount"] = map<string, boost::any>;

You can not assign type to object. To fix problem add ()

accounts["brokerageAccount"] = map<string, boost::any>();

Variant that should be compiled correctly is:

#include <boost/any.hpp>
#include <map>
#include <string>

int main()
{
   std::map<std::string, boost::any> accounts;
   accounts["cash"] = 100;
   accounts["brokerageAccount"] = std::map<std::string, boost::any>();
}

OTHER TIPS

map<string, boost::any> on the last line is a type, not an object of that type. You have to call the constructor of that type to create an argument. Change the last line to

accounts["brokerageAccount"] = map<string, boost::any>();

This fixes it on my copy of Visual Studio 2010

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top