简体   繁体   English

无法访问在类“ CObject”中声明的私有成员?

[英]Cannot access private member declared in class 'CObject'?

class EventDataValue {
public:
    enum Types {
        NONE,
        INT,
        STRING,
        DOUBLE,
        ULONG
    };

    EventDataValue() { this->Type = NONE; }
    EventDataValue( const EventDataValue &s ) { 
        this->Type = s.Type;

        if (this->Type == INT) 
            this->i = s.i;
        else if (this->Type == STRING)
            this->s = s.s;
        else if (this->Type == DOUBLE)
            this->d = s.d;
        else if (this->Type == ULONG)
            this->ul = s.ul;
    }

    EventDataValue& operator=( const EventDataValue &s ) { 
        this->Type = s.Type;

        if (this->Type == INT) 
            this->i = s.i;
        else if (this->Type == STRING)
            this->s = s.s;
        else if (this->Type == DOUBLE)
            this->d = s.d;
        else if (this->Type == ULONG)
            this->ul = s.ul;

        return *this; 
    }


    EventDataValue(int value) {
        this->Type = INT;
        this->i = value;
    }

    EventDataValue(CString &value) {
        this->Type = STRING;
        this->s = value;
    }

    EventDataValue(LPCTSTR value) {
        this->Type = STRING;
        this->s = CString(value);
    }

    EventDataValue(double value) {
        this->Type = DOUBLE;
        this->d = value;
    }

    EventDataValue(unsigned long value) {
        this->Type = ULONG;
        this->ul = value;
    }

    ~EventDataValue() { }

    operator int(void) const {
        return this->i;
    }

    operator CString(void) const {
        return this->s;
    }

    operator double(void) const {
        return this->d;
    }

    operator unsigned long(void) const {
        return this->ul;
    }


    Types Type;
    int i;
    CString s;
    double d;
    unsigned long ul;
};

class EventData {
public:
    EventData(CString strEventCode, CString &strSessionId, int nFlowId = 0) {
        this->Add(CString(_T("tp")), strEventCode);
        this->Add(CString(_T("ss")), strSessionId);
        this->Add(CString(_T("ts")), (int)std::time(0));

        if (nFlowId != 0)
            this->Add(CString(_T("fl")), nFlowId);
    }

    template <typename T>
    void Add(CString name, T value) {
        EventDataValue event_value(value);

        if (cMap.Lookup(name, NULL) == TRUE)
            return;

        cMap[name] = value;
    }

    CMap<CString, CString, EventDataValue, EventDataValue> cMap;
};

class Events {
public:
    Events() { }
    ~Events() { }

    void Add(EventData ev) {
        this->aEvents.Add(ev);
    }

    CStringW Serialize() {
        CStringW strOutput;
        INT_PTR i, j;

        if (_tcscmp(API_FORMAT, FORMAT_JSON) == 0) {
            for (i = 0; i != this->aEvents.GetCount(); i++) {
                EventData ev = this->aEvents[i];

                strOutput += L"{";

                j = 0;

                POSITION pos = ev.cMap.GetStartPosition();

                while (pos != NULL) {
                    CString key;
                    EventDataValue value;

                    ev.cMap.GetNextAssoc( pos, key, value );

                    strOutput += StringFormat(_T("\"%s\": "), key.GetString());

                    if (value.Type == EventDataValue::STRING) {
                        CString str = value;
                        strOutput += Enquoute(str);
                    } else if (value.Type == EventDataValue::INT) {
                        int n = value;
                        strOutput += StringFormat(_T("%d"), n);
                    } else if (value.Type == EventDataValue::DOUBLE) {
                        double d = value;
                        strOutput += StringFormat(_T("%d"), static_cast<int>(d));
                    } else if (value.Type == EventDataValue::ULONG) {
                        ULONG ul = value;
                        strOutput += StringFormat(_T("%u"), ul);
                    }

                    if (j++ < ev.cMap.GetCount() - 1)
                        strOutput += _T(",");
                }

                strOutput += _T("}");
            }
        } else {
            for (i = 0; i != this->aEvents.GetCount(); i++) {
                EventData ev = this->aEvents[i];

                strOutput += _T("<Event>");

                j = 0;

                POSITION pos = ev.cMap.GetStartPosition();

                while (pos != NULL) {
                    CString key;
                    EventDataValue value;

                    ev.cMap.GetNextAssoc( pos, key, value );

                    strOutput += StringFormat(_T("<%s>"), key.GetString());

                    if (value.Type == EventDataValue::STRING) {
                        CString str = value;
                        strOutput += str;
                    } else if (value.Type == EventDataValue::INT) {
                        int n = value;
                        strOutput += StringFormat(_T("%d"), n);
                    } else if (value.Type == EventDataValue::DOUBLE) {
                        double d = value;
                        strOutput += StringFormat(_T("%d"), static_cast<int>(d));
                    } else if (value.Type == EventDataValue::ULONG) {
                        ULONG ul = value;
                        strOutput += StringFormat(_T("%u"), ul);
                    }

                    strOutput += StringFormat(_T("</%s>"), key.GetString());
                }

                strOutput += _T("</Event>");
            }

        }

        return strOutput;
    }

    CArray<EventData> aEvents;
};

Can someone please tell me why I am getting the following error when I try to compile this? 有人可以告诉我为什么在尝试编译此错误时会出现以下错误吗?

1>c:\program files (x86)\microsoft visual studio 11.0\vc\atlmfc\include\afxtempl.h(1329): error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\atlmfc\include\afx.h(559) : see declaration of 'CObject::CObject'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\atlmfc\include\afx.h(534) : see declaration of 'CObject'
1>          This diagnostic occurred in the compiler generated function 'CMap<KEY,ARG_KEY,VALUE,ARG_VALUE>::CMap(const CMap<KEY,ARG_KEY,VALUE,ARG_VALUE> &)'
1>          with
1>          [
1>              KEY=CString,
1>              ARG_KEY=CString,
1>              VALUE=EventDataValue,
1>              ARG_VALUE=EventDataValue
1>          ]
void Add(EventData ev) {
    this->aEvents.Add(ev);
}

This must be Add(EventData& ev) . 必须Add(EventData& ev) Your EventData class has no non-default copy constructor or copy assignment operator and has a member ( CMap ) that is non-copyable. 您的EventData类没有非默认的复制构造函数或复制赋值运算符,并且具有不可复制的成员( CMap )。 This makes EventData non-copyable. 这使EventData不可复制。 So you can't pass it by value because that would have to make a copy, which you can't do since the class is non-copyable. 因此,您不能按值传递它,因为那样就必须创建一个副本,而该类是不可复制的,因此您不能这样做。

CMap has default copy-ctor. CMap具有默认的copy-ctor。 Since CMap is derived from CObject and CObject 's copy c-tor is private you get an error. 由于CMap是从CObject派生的,并且CObject的副本c-tor是私有的,因此会出现错误。

我没有使用CMapCArray ,而是使用了std::mapstd::vector因为CStringCMapCArray不兼容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;CObject :: CObject&#39;:无法访问在类&#39;CObject&#39;中声明的私有成员 - 'CObject::CObject' : cannot access private member declared in class 'CObject' 无法访问在类“ CObject”中声明的私有成员 - Cannot access private member declared in class 'CObject' VS2013编译器:“ CObject :: CObject”:无法访问在类“ CObject”中声明的私有成员 - VS2013 compiler: 'CObject::CObject' : cannot access private member declared in class 'CObject' 错误C2248:'CObject :: CObject':无法访问类'CObject'afxwin.h中声明的私有成员 - error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' afxwin.h 使用CArray时出错:无法访问在类CObject中声明的私有成员 - Error using CArray: cannot access private member declared in class CObject CPtrList 无法使用:错误 C2248:“CObject::operator =”:无法访问类“CObject”中声明的私有成员 - CPtrList cannot use: error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' 试图传递CStringArray给出错误无法访问类'CObject'中声明的私有成员 - Trying to pass a CStringArray gives error cannot access private member declared in class 'CObject' 向CArray添加数据会产生错误“无法访问在类&#39;CObject&#39;中声明的私有成员” - Adding data to CArray gives error “cannot access private member declared in class 'CObject'” &#39;CObject :: CObject&#39;:无法访问在&#39;CObject&#39;d:\\ Program Files \\ Microsoft Visual Studio 9.0 \\ vc \\ atlmfc \\ include \\ afxcoll.h中声明的私有成员 - 'CObject::CObject' : cannot access private member declared in class 'CObject'd:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxcoll.h 错误 C2248: 'CObject::CObject': 当我在 ZD7421054471AB272ZCEAC18FD97BBD237 - error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' when I calling hDC.SelectObject function in MFC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM