i have declared map variable in header file , trying insert values method in cpp file.
in header (.h
) file,
class test { public: void addname(const std::string& name, const std::string& value); private: std::map<std::string, std::string> m_names; };
in .cpp
file,
void test::addname(const std::string& name, const std::string& value) { m_names.insert(std::pair<std::string, std::string>(name, value)); }
this method throws error: "0xc0000005: access violation reading location 0x0000000000000150."
but if declare map variable in addname method
, no error.
i calling addname method class required parameters.
testptr test = nullptr; test->addname(nodedetails.attribute("name"), nodedetails.attribute("value"));
what issue?
the test
object has instancied before being used :
testptr test; test.addname(...);
or dynamically allocated (using new
)
testptr* test = new testptr(); test->addname(...); // //... // //and don't forget free memory delete test;
(in case, first method more "memory safe" ;) )
Comments
Post a Comment