【问题标题】:Adding class object in stl map as a value在 stl 映射中添加类对象作为值
【发布时间】:2014-12-15 05:17:26
【问题描述】:

我想在 STL 映射中添加类对象作为 C++ 中的值。就像std::map<CString,class myClass*>myMap。但是编译器向我展示了这样做的错误。我是否必须为该类实现所有比较运算符重载?如果没有,那么我该如何实现呢? 代码如下: // 头文件

#pragma once
#include "afxsock.h"
#include"NetworkDataProcessor.h"
#include"MainFrm.h"
#include"ChattingDialog.h"
#include<map>
using namespace std;

class CConnectionManager :public CAsyncSocket
{
public:
    static CConnectionManager *GetClientInstance();
    BOOL ClientSignIn(CString, CString);
    void ConnectToServer();
public:
    CString  m_sendBuffer; 
    int    m_nBytesSent; 
    int   m_nBytesBufferSize = MAX_BUFFER_SIZE;
    virtual void OnClose(int nErrorCode);
    virtual void OnConnect(int nErrorCode);
    virtual void OnReceive(int nErrorCode);
    virtual void OnSend(int nErrorCode);
public:

    std::map<CString, CChattingDialog* >ChatWindows;
private:`enter code here`
    CConnectionManager();
    ~CConnectionManager();
    static CConnectionManager * client_instance;
};

// cpp文件函数:

void CMyMessangerView::OnClientListClick(NMHDR* pnmh, LRESULT* pResult)
{
    DWORD dwPos = ::GetMessagePos();
    CPoint point((int)LOWORD(dwPos), (int)HIWORD(dwPos)); 
    GetListCtrl().ScreenToClient(&point);  int nIndex; if ((nIndex = GetListCtrl().HitTest(point)) != -1)
    {
    CString string = GetListCtrl().GetItemText(nIndex, 0);
    CChattingDialog chatingDlg;
    chatingDlg.SendToUser = string;
    CString user = chatingDlg.UserRealName(string);
    CConnectionManager *client = CConnectionManager::GetClientInstance();
    client->ChatWindows.insert(pair<CString, CChattingDialog *>(user, &chatingDlg));
    UpdateData(FALSE);
    chatingDlg.DoModal();
    } 
    *pResult = 0;

}

错误: 15 IntelliSense:没有重载函数实例“std::map<_kty _ty _pr _alloc>::insert [with _Kty=CString, _Ty=CChattingDialog *, _Pr=std::less, _Alloc=std::allocator> ]" 匹配参数列表 参数类型是:(std::pair) 对象类型为:std::map, std::allocator>

错误 3 错误 C2976:'std::map':模板参数太少 c:\projects\poc\mymessanger\mymessanger\clientconnection.h 25 1 MyMessanger

错误 4 错误 C2665: 'std::pair::pair' : 3 个重载都不能转换所有参数类型 c:\projects\poc\mymessanger\mymessanger\mymessangerview.cpp 131 1 MyMessanger 16 IntelliSense:没有构造函数实例“std::pair<_ty1 _ty2>::pair [with _Ty1=CString, _Ty2=CChattingDialog &]”与参数列表匹配 参数类型为: (CString, CChattingDialog *) c:\Projects\POC\MyMessanger\MyMessanger\MyMessangerView.cpp 131 29 MyMessanger 等等......还有一些错误,比如表示相同

【问题讨论】:

  • 向我们展示代码和确切的错误消息。
  • std::mapmyMap. “class myClass”看起来很奇怪。为什么要添加“类”。
  • 我需要收集此类对象以及作为字符串的键。我想根据一些字符串类型的键来获取该类的对象。
  • @MahendraChhimwal 除了语法错误(仍然不清楚哪一行有错误)之外,您正在将指针存储到映射中的局部变量。这些变量在它们声明的块之外不存在,所以这段代码有问题:client-&gt;ChatWindows.insert(pair&lt;CString, CChattingDialog *&gt;(user, &amp;chatingDlg)
  • 另外,不要发布 Intellisense 错误。发布编译器错误。

标签: c++ class map insert


【解决方案1】:

感谢大家对此问题的回复。谢谢福明·阿塞尼。 这个问题的解决方案是我在问题中猜到的,Fomin Arseniy 在上面说。 我们必须至少为要在 map 中使用的类重载复制构造函数和赋值运算符作为值。 首先,用户定义数据类型的映射声明需要像

std::map<CString, class CChattingDialog> ChatWindows;

而不是

std::map<CString, CChattingDialog> ChatWindows;

第二,我添加了两个函数

 CChattingDialog& operator=(const CChattingDialog &s); 

 CChattingDialog(const CChattingDialog &s);

在 CChattingDialog 类中。使用 Fomin Arseniy 建议的插入方法。

client->ChatWindows[user] = &chatingDlg;

成功编译代码。

如果需要在 STL map 中添加用户定义的数据类型,我们必须提供公共构造函数、复制构造函数、析构函数、assining 运算符和运算符

【讨论】:

    【解决方案2】:

    您以错误的方式使用 std::map。你应该重写你的代码如下:

    client->ChatWindows[user] = &chatingDlg;
    

    (如果您想使用 map::insert 方法,您可以在此处阅读:http://www.cplusplus.com/reference/map/map/insert/。没有像您那样插入对,有一种方法返回一对 interator/succes)。

    但是,您还询问了您需要在类中实现什么才能将其存储在 map 中,而不是通过指针而是通过值:

    std::map<CString, CChattingDialog> ChatWindows;
    

    正确答案是:CChattingDialog 类中需要公共构造函数、复制构造函数、析构函数、assining 运算符和运算符

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      相关资源
      最近更新 更多