2 chenyg5 chenyg5 于 2015.06.10 16:38 提问

c++重载运算符 +=报错

typedef struct UserInfo{
ULONGLONG m_ullReqNum;
ULONGLONG m_ullResNum;
ULONGLONG m_ullEmptyResNum;

}USERINFO_S;

typedef struct UserQueryReport{

std::map<std::string,USERINFO_S>m_strUserInfo;

void operator +=(const UserQueryReport& stReport) {
        std::map<std::string,USERINFO_S>::iterator itr;
        for( itr = stReport.m_strUserInfo.begin();itr!= stReport.m_strUserInfo.end();itr++)
        {
            m_strUserInfo[itr->first].m_ullReqNum+= stReport.m_strUserInfo[itr->first].m_ullReqNum ;
            m_strUserInfo[itr->first].m_ullResNum+= stReport.m_strUserInfo[itr->first].m_ullResNum ;
            m_strUserInfo[itr->first].m_ullEmptyResNum+= stReport.m_strUserInfo[itr->first].m_ullEmptyResNum ;

        }

 }

}UESRQUERYRPORT_S;

4个回答

devmiao
devmiao   Ds   Rxr 2015.06.10 16:58

error: no match for ‘operator=’
你看你哪里调用等号的。
贴出完整代码。main函数在哪

wsh1992o
wsh1992o   Rxr 2015.06.10 16:41

顺便把错误代码发出来一下

chenyg5
chenyg5   2015.06.10 16:42

myoo.cpp: In member function ‘void UserQueryReport::operator+=(const UserQueryReport&)’:
myoo.cpp:21: error: no match for ‘operator=’ in ‘itr = stReport->UserQueryReport::m_strUserInfo.std::map<_Key, _Tp, _Compare, _Alloc>::begin with _Key = std::basic_string, std::allocator >, _Tp = UserInfo, _Compare = std::less, std::allocator > >, _Alloc = std::allocator, std::allocator >, UserInfo> >
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:154: note: candidates are: std::_Rb_tree_iterator, std::allocator >, UserInfo> >& std::_Rb_tree_iterator, std::allocator >, UserInfo> >::operator=(const std::_Rb_tree_iterator, std::allocator >, UserInfo> >&)
myoo.cpp:23: error: passing ‘const std::map, std::allocator >, UserInfo, std::less, std::allocator > >, std::allocator, std::allocator >, UserInfo> > >’ as ‘this’ argument of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator [with _Key = std::basic_string, std::allocator >, _Tp = UserInfo, _Compare = std::less, std::allocator > >, _Alloc = std::allocator, std::allocator >, UserInfo> >]’ discards qualifiers
myoo.cpp:24: error: passing ‘const std::map, std::allocator >, UserInfo, std::less, std::allocator > >, std::allocator, std::allocator >, UserInfo> > >’ as ‘this’ argument of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator [with _Key = std::basic_string, std::allocator >, _Tp = UserInfo, _Compare = std::less, std::allocator > >, _Alloc = std::allocator, std::allocator >, UserInfo> >]’ discards qualifiers
myoo.cpp:25: error: passing ‘const std::map, std::allocator >, UserInfo, std::less, std::allocator > >, std::allocator, std::allocator >, UserInfo> > >’ as ‘this’ argument of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator [with _Key = std::basic_string, std::allocator >, _Tp = UserInfo, _Compare = std::less, std::allocator > >, _Alloc = std::allocator, std::allocator >, UserInfo> >]’ discards qualifiers
myoo.cpp: In function ‘int main()’:

chenyg5
chenyg5   2015.06.11 11:49

去掉const 问题解决,但不知道原因

Csdn user default icon
上传中...
上传图片
插入图片