首页 > 解决方案 > Passing Map by Reference Conflicting Messages

问题描述

I have the following class header (modified for brevity):

#include <map>
#include <vector>
#include <string>

class ComMon
{
    //Properties
    public:
    typedef std::map<int, std::map<int, int>> CMD_Map;
    //<Level_Number, <Member_ID, Role_ID (default=0)>>
    static int Add_Platform(int LevelNO,int MemberID, int RoleID,CMD_Map *CurrentMap);

......
}

And the associated cpp file (modified for brevity):

int ComMon::Add_Platform(int LevelNO, int MemberID, int RoleID, ComMon::CMD_Map *CurrentMap)
{
    if (CurrentMap->empty) {
        std::map<int, int> InnerMap;
        InnerMap.insert_or_assign(MemberID, RoleID);
        CurrentMap->insert_or_assign(LevelNO, InnerMap);
    }
    else {
        //see if Level is in Map
        int Levels = (CurrentMap->size);
        if (Levels >= LevelNO) {
            //get inner list for the level
            CurrentMap[LevelNO].insert_or_assign(MemberID,RoleID);
        }
        else {
            //add new level
            std::map<int, int> InnerMap;
            InnerMap.insert_or_assign(MemberID, RoleID);
            CurrentMap[LevelNO].insert_or_assign(LevelNO, InnerMap);
        }
        }


        return 0;
    }

When I compile, I get the following error message:

Error C3867 'std::_Tree>::empty': non-standard syntax; use '&' to create a pointer to member

With the error pointing to this:

if (CurrentMap->empty) {

If I alter the line to this:

if (&CurrentMap->empty) {

and try to compile, I get the following error message:

'&': illegal operation on bound member function expression

Again, pointing to the same line of code.

Can anyone help?

标签: c++

解决方案


empty is an object method; you want to call it, not get its memory address. To make a function call, add trailing parenthesis to specify a parameter list to pass in to the call (in this case, a parameter list with no values in it), eg:

if (CurrentMap->empty()) {

推荐阅读