
I'm having difficulty storing pointers in a std::map. I can't find anything online about it, are std::maps supposed to be able to store pointers?

Value. Like this. #include <iostream> #include <map> void func(int* n) { std::cout<<n<<" "<<*n<<std::endl; std::map<std::string, int*> m1; //m1["test"] = n; } void main() { func(new int()); } Uncomment the m1... line to uncover the error. On 11 March 2010 17:25, Rutger ter Borg <rutger@terborg.net> wrote:
Alan Tennant wrote:
I'm having difficulty storing pointers in a std::map. I can't find anything online about it, are std::maps supposed to be able to store pointers?
Sure, as key or as value? Providing a code example might help in this case.
Regards,
Rutger
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

What's the error ? It compiles fini here with gcc 4.4 (except the fact I had to include string), and I can't see any reason why it won't compile fine...

On 11 March 2010 12:40, Alan Tennant <alan2here@gmail.com> wrote:
Uncomment the m1... line to uncover the error.
First, this is not really a boost question, so a general C++ list would be a more appropriate forum. That said, running the code -- with the line uncommented -- through codepad gives me only one error: Line 12: error: '::main' must return 'int' I suspect you're using a less-than-stellar compiler. ~ Scott

Thanks. I presume it would be better in implementing a map that can hold left = std::string, right = "any" but only pointers to objects to have a boost::map_ptr<std::string> than a std::map<std::string, boost::any>. On 11 March 2010 21:06, Thorsten Ottosen <thorsten.ottosen@dezide.com>wrote:
Alan Tennant skrev:
Thanks guys. I'm embarrassed to say that it was the #include <string>
problem.
use
boost::ptr_map<std::string,int>
or
std::map<std::string,boost::shared_ptr<int>>
to manange the memory, if you need that.
-Thorsten
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (5)
-
Alan Tennant
-
Mathieu -
-
Rutger ter Borg
-
Scott McMurray
-
Thorsten Ottosen