How to insert and retrive an object without default ctor from container?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: How to insert and retrive an object without default ctor from container?

  1. #1
    Join Date
    Jul 2002
    Posts
    759

    How to insert and retrive an object without default ctor from container?

    Hi,

    I have a third party class object, CPP_OBJECT that i save in std container.
    for example; i want to do the following.

    std::map<std::string, CPP_OBJECT> mymap;
    mymap["test"]= myobj;

    mymap.insert["test",myobject];//This doesn't work!!


    Now that I have created the CPP_OBJECT and save it to the map.

    i want to retrieve the CPP_OBJECT from the map using a std::string ID later.

    CPP_OBJECT l_object = mymap["test"];
    apparent i can't do as ablve as the CPP_OBJECT does't have a default ctor.

    CPP_OBJECT *l_object = &mymap["test"]; //this result in error "no default constructor available" in CPP_OBJECT,

    Can anyone help me on this??

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,246

    Re: How to insert and retrive an object without default ctor from container?

    The operator[] of std::map creates an object (using the default constructor) if none is found at the given key. Since this is undesirable here, use the find() member function of std::map instead.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2002
    Posts
    759

    Re: How to insert and retrive an object without default ctor from container?

    i tried the following:
    Code:
    mymap["test"]=mycppobj;
    
    std::map<std::string,CPP_OBJECT>::iterator it=mymap.find("test");
    std::pair<std::string,CPP_OBJECT> pair=*it;
    CPP_OBJECT* t=pair.second(); //i still getting error : call of an object wihtout appropriate operator() or conversion functions to pointer-to-function type...
    could you show me a simple working codes?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,246

    Re: How to insert and retrive an object without default ctor from container?

    It should be something like:
    Code:
    std::map<std::string,CPP_OBJECT>::iterator it = mymap.find("test");
    if (it != mymap.end())
    {
        std::cout << *it << std::endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Apr 1999
    Posts
    27,423

    Re: How to insert and retrive an object without default ctor from container?

    Quote Originally Posted by mce View Post
    could you show me a simple working codes?
    There are thousands of examples in books, on the Internet, etc. If you're going to use the container classes, you should have reference material ready to use instead of guessing what the code should be:

    http://www.cplusplus.com/reference/stl/map/find/

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,281

    Re: How to insert and retrive an object without default ctor from container?

    Quote Originally Posted by mce View Post
    i tried the following:
    Code:
    mymap["test"]=mycppobj;
    
    std::map<std::string,CPP_OBJECT>::iterator it=mymap.find("test");
    std::pair<std::string,CPP_OBJECT> pair=*it;
    CPP_OBJECT* t=pair.second(); //i still getting error : call of an object wihtout appropriate operator() or conversion functions to pointer-to-function type...
    could you show me a simple working codes?
    Almost right, but "second" is just a struct member variable, not a function, so it should be:

    Code:
    std::map<std::string,CPP_OBJECT>::iterator it=mymap.find("test");
    std::pair<std::string,CPP_OBJECT> pair=*it;
    CPP_OBJECT* t=pair.second;//No parenthesis here
    ----

    Quote Originally Posted by laserlight View Post
    It should be something like:
    Code:
    std::map<std::string,CPP_OBJECT>::iterator it = mymap.find("test");
    if (it != mymap.end())
    {
        std::cout << *it << std::endl;
    }
    That won't work, "it" is an iterator to a key-value pair, that isn't streamable. it should be (if you wanted to print the pair):

    Code:
    std::map<std::string,CPP_OBJECT>::iterator it = mymap.find("test");
    if (it != mymap.end())
    {
        std::cout << it->first << " " << it->second << std::endl;
    }
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,246

    Re: How to insert and retrive an object without default ctor from container?

    Quote Originally Posted by monarch_dodra
    That won't work, "it" is an iterator to a key-value pair, that isn't streamable. it should be (if you wanted to print the pair):
    Yeah, my bad, I didn't test.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center