1
votes

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

2
Show us the code and the exact error message.0x499602D2
std::map<CString,class myClass*>myMap. "class myClass" looks weird. Why are you adding "class".user1
I need collection of this class objects along with key as string. i want to get object of that class according to some string type keys.Mahendra Chhimwal
@MahendraChhimwal Other than the syntax error (which still isn't clear which line has the error), you're storing pointers to local variables in the map. Those variables do not exist outside of the block they're declared in, so this code is faulty: client->ChatWindows.insert(pair<CString, CChattingDialog *>(user, &chatingDlg)PaulMcKenzie
Also, don't post Intellisense errors. Post compiler errors.PaulMcKenzie

2 Answers

3
votes

Thanks everyone for responding to this problem. Thank you Fomin Arseniy. The solution to this problem is what I guessed in the question and Fomin Arseniy said above. We must have to overload at least Copy constructor and assigning operator for class we are going to use in map as a value. First ,map declaration for user defined data types needs to be like

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

instead of

std::map<CString, CChattingDialog> ChatWindows;

and second,I added two functions

 CChattingDialog& operator=(const CChattingDialog &s); 

 CChattingDialog(const CChattingDialog &s);

in the class CChattingDialog . Used inserting method as advised by Fomin Arseniy.

client->ChatWindows[user] = &chatingDlg;

compiled the code successfully.

We must have to provide public constructor, copy constructor, desctructor, assining operator and operator< (less than) if need to add user defined data types in STL map.

2
votes

You use std::map in wrong way. You should rewrite your code as follows:

client->ChatWindows[user] = &chatingDlg;

(If you want to use map::insert method you can read about it here: http://www.cplusplus.com/reference/map/map/insert/. There is no pair inserting as you do, there is one method that returns pair of interator/succes).

However, you also asked what do you need to implement in your class to store it in map not by pointer but by value as:

std::map<CString, CChattingDialog> ChatWindows;

Correct answer is: you need public constructor, copy constructor, desctructor, assining operator and operator< (less than) in class CChattingDialog but not in class CConnectionManager. Map uses them to have possibility to correctly store, copy, delete and sort elements of this class in it.