c++ - error: invalid use of 'Config::testMap' -


here code:

#include <iostream> #include <string> #include <map> #include <stdexcept> #include <boost/ptr_container/ptr_vector.hpp>  struct teststruct {     std::string str1;     int var1; };  struct config {     // map     typedef std::map< std::string, boost::ptr_vector<struct teststruct> > testmap; };  void foo(config& config) {     if (config.testmap.empty())     {         std::cout << "it worked!" << std::endl;     }     else     {         std::cout << "it didn't work!" << std::endl;     }     return; }  int testmain(void) {     config config;      foo(config);      return; }  int main(void) {     try     {         return testmain(/*argc, argv*/);     }     catch(std::exception& err)     {         std::cerr << "error running program: " << err.what() << std::endl;         return 1;     }     catch(...)     {         std::cerr << "program failed unknown exception." << std::endl;         return 1;     } } 

i'm new maps - never used 1 before. i've found many examples of how use them online. unfortunately can't seem makes sense of more advanced examples of them.

what i'd create map key (std::string) , value (boost::ptr_vector<struct>).

i going start declaring , passing around successfully. wanted try , figure out how fill up.

i ran error vague enough don't know how interpret it.

any suggestions on i've done wrong in "use" of testmap?

also, can provide simple example of how can populate map up.

say want key of a , value of str1 = "hello", var1 = 10. how this?

follow question: in regards answer left below kerrek sb.

if following...

std::string key   = "a"; teststruct value = {"hello", 10}; config.testmap[key] = value; 

i following error:

 error: no match 'operator=' in 'config->config::testmap.std::map<_key, _tp, _compare, _alloc>::operator[] [with _key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _tp = boost::ptr_vector<teststruct, boost::heap_clone_allocator, std::allocator<void*> >, _compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::ptr_vector<teststruct, boost::heap_clone_allocator, std::allocator<void*> > > >](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& key)))) = value' /usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note: candidates are: boost::ptr_vector<t, cloneallocator, allocator>& boost::ptr_vector<t, cloneallocator, allocator>::operator=(std::auto_ptr<boost::ptr_vector<t, cloneallocator, allocator> >) [with t = teststruct, cloneallocator = boost::heap_clone_allocator, allocator = std::allocator<void*>] /usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note:                 boost::ptr_vector<t, cloneallocator, allocator>& boost::ptr_vector<t, cloneallocator, allocator>::operator=(boost::ptr_vector<t, cloneallocator, allocator>) [with t = teststruct, cloneallocator = boost::heap_clone_allocator, allocator = std::allocator<void*>] 

if instead .insert() method following error:

instantiated here /opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/../../../../include/c++/4.3.2/bits/stl_pair.h:106: error: no matching function call 'boost::ptr_vector<teststruct, boost::heap_clone_allocator, std::allocator<void*> >::ptr_vector(const teststruct&)' /usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:50: note: candidates are: boost::ptr_vector<t, cloneallocator, allocator>::ptr_vector(typename boost::ptr_sequence_adapter<t, std::vector<void*, allocator>, cloneallocator>::size_type, const typename boost::ptr_sequence_adapter<t, std::vector<void*, allocator>, cloneallocator>::allocator_type&) [with t = teststruct, cloneallocator = boost::heap_clone_allocator, allocator = std::allocator<void*>] /usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note:                 boost::ptr_vector<t, cloneallocator, allocator>::ptr_vector(std::auto_ptr<boost::ptr_vector<t, cloneallocator, allocator> >) [with t = teststruct, cloneallocator = boost::heap_clone_allocator, allocator = std::allocator<void*>] /usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note:                 boost::ptr_vector<t, cloneallocator, allocator>::ptr_vector(const typename boost::ptr_sequence_adapter<t, std::vector<void*, allocator>, cloneallocator>::allocator_type&) [with t = teststruct, cloneallocator = boost::heap_clone_allocator, allocator = std::allocator<void*>] /usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note:                 boost::ptr_vector<t, cloneallocator, allocator>::ptr_vector() [with t = teststruct, cloneallocator = boost::heap_clone_allocator, allocator = std::allocator<void*>] /usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:35: note:                 boost::ptr_vector<teststruct, boost::heap_clone_allocator, std::allocator<void*> >::ptr_vector(const boost::ptr_vector<teststruct, boost::heap_clone_allocator, std::allocator<void*> >&) 

follow up:

from i've researched not possible.

you cannot use ptr_vector in map. (so i've been told) due ownership/copy issues?

"what happens when attempt copy ptr_vector, bound happen inside map? pointer containers model exclusive ownership of pointers.

you can c++0x , std::move, won’t here."

can provide counter example?

your class config doesn't contain members! (just typedef.) define member, too:

struct config {     typedef std::map<std::string, boost::ptr_vector<teststruct> > testmap_type;      testmap_type testmap; // member }; 

also, in c++ there no need struct teststruct, teststruct.

to add element:

std::string key = "xxx"; teststruct  val = { "hello", 10 };  // insert []-operator config.testmap[key] = val;  // insert insert(): config.testmap.insert(std::pair<std::string, teststruct>(key, val)); // alternative 

edit: sorry, misrepresented actual data structure. here's example std::vector:

typedef std::map<std::string, std::vector<int>> mymap; mymap m;  m["hello"].push_back(1); m["hello"].push_back(2); m["world"].push_back(3); 

in case, config.testmap[key].push_back(val), or make new vector:

boost::ptr_vector<teststruct> new_v; // populate new_v; config.testmap.insert(std::pair<std::string, boost::ptr_vector<teststruct>>(key, new_v); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -