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

Posted

技术标签:

【中文标题】无法访问在类“CObject”中声明的私有成员?【英文标题】:Cannot access private member declared in class 'CObject'? 【发布时间】:2012-12-24 08:06:21 【问题描述】:
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;
;

有人可以告诉我为什么我在尝试编译时收到以下错误吗?

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>          ]

【问题讨论】:

CObject 是不可复制的,这意味着您只需要通过引用传递和返回它,而不是通过值。这意味着您不能复制派生自CObject 的对象或包含派生自CObject 的对象的对象。 【参考方案1】:
void Add(EventData ev) 
    this->aEvents.Add(ev);

这个必须Add(EventData&amp; ev)。您的 EventData 类没有非默认的复制构造函数或复制赋值运算符,并且有一个不可复制的成员 (CMap)。这使得EventData 不可复制。所以你不能按值传递它,因为那必须制作一个副本,你不能这样做,因为该类是不可复制的。

【讨论】:

【参考方案2】:

CMap 有默认的copy-ctor。由于CMap 派生自CObject 并且CObject 的副本c-tor 是私有的,因此您会收到错误。

【讨论】:

【参考方案3】:

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

【讨论】:

不真实。我在一些应用程序中广泛使用它。从我的代码中获取实际应用程序的示例:CMap&lt;CString, LPCTSTR, CMappedPoints*, CMappedPoints*&amp;&gt; MappedPointsCArray&lt;CString&gt; Tags

以上是关于无法访问在类“CObject”中声明的私有成员?的主要内容,如果未能解决你的问题,请参考以下文章

错误 C2248:“CObject::CObject”:当我在 MFC 中调用 hDC.SelectObject 函数时,无法访问在“CObject”类中声明的私有成员

查找“无法访问在类'QObject'中声明的私有成员”的根本原因

使用受保护和继承时无法访问在类中声明的私有成员

编译器错误 C2248:“QObject::Qobject”:无法访问在类“QObject”中声明的私有成员

绕过错误 C2248“无法访问在类中声明的受保护成员”的有效方法

什么是友元?