1
我是新的C++。我试图在CPP加龙对象,但我同时加入了对象在C++中添加字符串和列表来映射
#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;
}
当我编译我在接收到错误的程序接收到错误:
dmap.insert(dmaptype(location, dlist));
和误差:
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> > >&)
任何帮助将不胜感激...
谢谢
HI它给出了同样的错误....项目的要求是使用地图,并尝试添加龙对象的名单到它与主要作为一个字符串。 – Baba 2012-02-13 04:18:06
你有没有修改你的dlist列表并摆脱新的等...?列表与列表不一样。 –
kfmfe04
2012-02-13 04:19:32
对不起我的坏...修复问题感谢堆 – Baba 2012-02-13 04:27:01