-
June 18th, 2012, 12:03 AM
#1
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??
-
June 18th, 2012, 12:09 AM
#2
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.
-
June 18th, 2012, 12:24 AM
#3
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?
-
June 18th, 2012, 12:36 AM
#4
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;
}
-
June 18th, 2012, 01:07 AM
#5
Re: How to insert and retrive an object without default ctor from container?
Originally Posted by mce
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
-
June 18th, 2012, 03:02 AM
#6
Re: How to insert and retrive an object without default ctor from container?
Originally Posted by mce
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
----
Originally Posted by laserlight
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 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.
-
June 18th, 2012, 03:06 AM
#7
Re: How to insert and retrive an object without default ctor from container?
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|