问题描述
- 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;
解决方案
error: no match for ‘operator=’
你看你哪里调用等号的。
贴出完整代码。main函数在哪
解决方案二:
顺便把错误代码发出来一下
解决方案三:
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()’:
解决方案四:
去掉const 问题解决,但不知道原因