1

I am new in c++. I am trying to add a dragon object in cpp but i am receiving an error while adding the object

#include "dragon.h"
using std::cin;
#include <map>
using std::map;
#include <list>
#include <string>
#include <iostream>
using namespace std;
using std::list;

typedef map<string,list<Dragon> >::iterator iter;
typedef map<const string,list<Dragon> >::value_type dmaptype;
void display(map<string,list<Dragon> > dmap1,iter dmapiter1);


int main( )
{
  map<string,list<Dragon> >dmap;
iter dmapiter;

bool again = true;

string name;
double length;
string colour;
string location;
while( again )
{
     // get the details for the dragon
     cout << "\nEnter dragon name >> ";
     getline( cin, name );

     cout << "\nEnter dragon length >> ";
     cin >> length;

     cin.get( );

     cout << "\nEnter dragon colour >> ";
     getline( cin, colour );

     // now get the location
     cout << "\nEnter location >> ";
     getline( cin, location );
     dmapiter=dmap.find(location);

    Dragon * ptr ;
    ptr=new Dragon(name,length,colour);

     if(dmapiter==dmap.end())
     {
        list<Dragon*> dlist;
        dlist.push_back(ptr);
        dmap.insert(dmaptype(location, dlist));
     }
     else
     {
        dmapiter->second.push_back(*ptr);
     }
     char choice;

     cout << "\nEnter another dragon and location [ y / n ] ? ";
     cin >> choice;
     cin.get( );
     if( choice != 'y' && choice != 'Y' )
     {
          again = false;
     }

}
 display(dmap,dmapiter);


cout << endl;

return 0;


}

when i compile the program i am receiving an error at:

dmap.insert(dmaptype(location, dlist));

and the error is :

 error: no matching function for call to ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::list<Dragon, std::allocator<Dragon> > >::pair(std::string&, std::list<Dragon*, std::allocator<Dragon*> >&)’
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:83: note: candidates are: std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = std::list<Dragon, std::allocator<Dragon> >]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:79: note:                 std::pair<_T1, _T2>::pair() [with _T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = std::list<Dragon, std::allocator<Dragon> >]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:68: note:                 std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::list<Dragon, std::allocator<Dragon> > >::pair(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::list<Dragon, std::allocator<Dragon> > >&)

Any help will be appreciated...

Thank you

1 Answer 1

2

Try:

dmap.insert(std::make_pair(location, dlist));

but you also need to make the type of dlist consistent with that of your map - use list<Dragon> or list<Dragon*> for both.

For simplicity (so you don't need to worry about deleting), try list<Dragon> first. When you insert into this list, a copy will be made (since STL has value-semantics), so it's ok if you create a Dragon locally on the stack to insert into dlist. (You can think of it as a copy being made: copy elision may occur if the compiler is smart, but don't worry about that, for now).

For example:

Dragon dragon(name,length,colour);

if(dmapiter==dmap.end())
{
  list<Dragon> dlist;
  dlist.push_back(dragon);
  dmap.insert(make_pair(location, dlist));
}
else
{
  dmapiter->second.push_back(dragon);
}
Sign up to request clarification or add additional context in comments.

3 Comments

HI it gives the same error....the requirement of the project is to use map and try to add list of dragon object into it with key as a string.
did you modify your dlist to list<Dragon> and get rid of the new, etc...? list<Dragon*> is not the same as list<Dragon>.
look at the snippet I added to the answer - your mistake is assuming that list<Dragon*> is the same as list<Dragon> - they are different types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.