I'm trying to use an map with string key, but it's not working and I couldn't figure out why. I would like to have some help to understand C++ fundamentals about the usage of this so essential structure.
model.hpp
#pragma once
#include <map>
#include <utility>
#include <string>
#include <iostream>
#include "../prs/ast.h"
using namespace std;
using namespace ast;
typedef map<string, Variable> Map;
typedef pair<string, Variable> Element;
namespace model{
    class Warehouse {
    public:
        Map stock;
        Warehouse(){
            Map* _stock = new Map();
            stock = *_stock;
        }
        Variable* get(string id){
            Map::iterator it = stock.find(id);
            if (it != stock.end()){
                return &(*it).second;
            }
            else {
                return __define__(id);
            }
        }
        Variable* __define__(string id){
            Variable* defined = new Variable(id);
            stock.insert(Element(id, *defined));
            return defined;
        }
    };
    static Warehouse* WAREHOUSE;
};
model.cpp
#pragma once
#include "model.hpp"
using namespace std;
namespace model {
    Warehouse* WAREHOUSE = new Warehouse();
}
In this context,
Variableis a project object defined inastnamespace already tested, as wellWAREHOUSEpointer is working accordingly, with class initialized
The instruction stock.find(id) is throwing the mentioned error message: Segmentation fault (core dumped), what I suppose means stock isn't correct initialized.
What is exactly happening with stock initialization done at Warehouse constructor? I understand that new keyword allocs the map and dereference its returned point would store the structure at stock Warehouse member attribute.
Am I misunderstand it?
Aucun commentaire:
Enregistrer un commentaire