2012-02-13 47 views
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> > >&) 

任何帮助将不胜感激...

谢谢

回答

2

尝试:

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

,但你也需要做出DLIST一致的类型与您的地图 - 使用两个list<Dragon>list<Dragon*>

为了简单起见(因此您不必担心删除),请先尝试list<Dragon>。 当你插入到这个列表中时,将会创建一个副本(因为STL具有值语义),所以如果你在堆栈上本地创建一个龙以插入到dlist中,那么可以。 (你可以把它看作是一个副本:如果编译器很聪明,复制elision可能会发生,但现在不用担心)。

例如:

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); 
} 
+0

HI它给出了同样的错误....项目的要求是使用地图,并尝试添加龙对象的名单到它与主要作为一个字符串。 – Baba 2012-02-13 04:18:06

+0

你有没有修改你的dlist列表并摆脱新的等...?列表与列表不一样。 – kfmfe04 2012-02-13 04:19:32

+0

对不起我的坏...修复问题感谢堆 – Baba 2012-02-13 04:27:01