I rote this code in my program

JSONNode::const_iterator iter = root.begin();
    for (; iter!=root.end(); ++iter)
    {
        const JSONNode& arrayNode = *iter;
        std::string type = arrayNode["type"].as_string();

        if(type == "node")
        {
            std::string id = arrayNode["id"].as_string();
            double lat = arrayNode["lat"].as_float();
            double lon = arrayNode["lon"].as_float();

            Node node;
            node.SetId(id);
            node.SetLatitude(lat);
            node.SetLongitude(lon);

            nodesMap.insert( std::pair<std::string, Node>(id, node) );
        }
        else if(type == "way")
        {
            std::string wayId = arrayNode["id"].as_string();
            wayNode.SetId(wayId);
            std::vector<Node> collection;

            const JSONNode& wayNodes = arrayNode["nodes"];
            const JSONNode& nodes = wayNodes.as_array();

            JSONNode::const_iterator WayIter = nodes.begin();
            for (; WayIter!=nodes.end(); ++WayIter)
            {
                const JSONNode& arrayNode = *WayIter;
                std::string id = arrayNode.as_string();

                if(nodesMap.find(id) != nodesMap.end())
                {
                    collection.push_back(nodesMap.find(id)->second);
                    nodesMap.erase(id);  
                }
            }

            wayNode.SetNodesCollection(collection);

            std::cout<<"Item Id ->>>>>>>>>>>>>" << collection[2].GetId() << std::endl;
        }
    }

Node.h

class Node {
private:
    std::string id;
    double latitude;
    double longitude;
public:
    Node();
    Node(const Node& orig);
    Node(std::string id, double lat, double lon);
    virtual ~Node();

    void SetLongitude(double longitude);
    double const & GetLongitude() const;

    void SetLatitude(double latitude);
    double const & GetLatitude() const;

    void SetId(std::string id);
    std::string const & GetId() const;
};

Node.cpp

Node::Node() {
}

Node::Node(const Node& orig) {
}

Node::~Node() {
}

Node::Node(std::string id, double lat, double lon){
    this->id = id;
    this->latitude = lat;
    this->longitude = lon;
}

void Node::SetLongitude(double longitude) {
    this->longitude = longitude;
}

double const & Node::GetLongitude() const {
    return longitude;
}

void Node::SetLatitude(double latitude) {
    this->latitude = latitude;
}

double const & Node::GetLatitude() const {
    return latitude;
}

void Node::SetId(std::string id) {
    this->id = id;
}

std::string const & Node::GetId() const {
    return id;
}

but when i try to ptint id of second item std::cout<<"Item Id ->>>>>>>>>>>>>" << collection[2].GetId() << std::endl;
it gets a blank value. but size of the collection is 82, get the correct value for the collection size.

i need some help to sort out this.
thanks in advance!

Instead of collection[2].GetId() use collection.at(2).GetId() the difference between these 2 calls is that calling at throws an exception if the index is out of range where using [] just does something undefined.

Assuming the index is in range then it is not a problem with the containers but what they contain. The data in collections is blank, which implies the data in nodesmap is blank which implies it isn't being read correctly.

Make sure you check all possible error code/results you can. For example from map::insert.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.