I want to add class object in STL map as a value in C++.It's like std::map<CString,class myClass*>myMap
. But compilers showing me error in doing this. Should I have to implement all comparison operators overloading for that class?? If not then how do I can achieve that?
Code is as follows:
// header file
#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 file function:
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;
}
ERRORS: 15 IntelliSense: no instance of overloaded function "std::map<_Kty, _Ty, _Pr, _Alloc>::insert [with _Kty=CString, _Ty=CChattingDialog *, _Pr=std::less, _Alloc=std::allocator>]" matches the argument list argument types are: (std::pair) object type is: std::map, std::allocator>
Error 3 error C2976: 'std::map' : too few template arguments c:\projects\poc\mymessanger\mymessanger\clientconnection.h 25 1 MyMessanger
Error 4 error C2665: 'std::pair::pair' : none of the 3 overloads could convert all the argument types c:\projects\poc\mymessanger\mymessanger\mymessangerview.cpp 131 1 MyMessanger 16 IntelliSense: no instance of constructor "std::pair<_Ty1, _Ty2>::pair [with _Ty1=CString, _Ty2=CChattingDialog &]" matches the argument list argument types are: (CString, CChattingDialog *) c:\Projects\POC\MyMessanger\MyMessanger\MyMessangerView.cpp 131 29 MyMessanger etc... few more errors like indicating the same
client->ChatWindows.insert(pair<CString, CChattingDialog *>(user, &chatingDlg)
– PaulMcKenzie