【问题标题】:Overriding Copy Constructor for CObList MFC覆盖 CObList MFC 的复制构造函数
【发布时间】:2013-06-20 19:16:10
【问题描述】:

我在 MFC 中工作,并且我有自己的模板类 (CDFAObList),它派生自 CObList,并且可以接受我自己的从 CObject 派生的类 (CDFAObject) 的成员。我需要为 CDFAObList 覆盖编译器生成的复制构造函数,因为它最终会向下运行到 CObject,它具有私有复制和赋值函数并给了我这个:

1>error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
1>see declaration of 'CObject::CObject'
1>see declaration of 'CObject'
1>This diagnostic occurred in the compiler generated function 'CObList::CObList(const CObList &)'

即使我重写了复制构造函数并且在 CDFAObject 中重载了赋值运算符,它也会给我上述错误。但是当我尝试覆盖 CDFAObList 的复制构造函数时,我得到以下编译器错误:

1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>see reference to class template instantiation 'CDFAObList<T>' being compiled

这是我的模板类:

#include "DFAObject.h"
#include "DFAManDoc.h"
#include "DFAManTreeView.h"

template<class T> class CDFAObList : public CObList
{
 public:
    CDFAObList(void) { }


    CDFAObList(CDocument* pDoc,CTreeCtrl* pTree, xml_document* pXmlDoc)
    {
        doc = pDoc;
        Tree = pTree;
        xmlDoc = pXmlDoc;
    }

    // problem copy constructor
    CDFAObList(const CDFAOblist<T>& toCopy)
    {
        doc = toCopy.doc;
        Tree = toCopy.tree;
        xmlDoc = toCopy.xmlDoc;

        for (int i = 0; i < toCopy->GetSize(); i++)
        {
            this->AddHead( (T*) toCopy->GetTail());
        }
    }

protected:
    CDocument* doc;
    CTreeCtrl* Tree;
    xml_document* xmlDoc;
};

我以前从未使用过类模板,所以我可能做错了很多事情。提前感谢您的帮助。

【问题讨论】:

    标签: c++ visual-studio visual-c++ mfc


    【解决方案1】:

    应该是

    CDFAObList(const CDFAObList<T>& toCopy)
    

    而不是

    CDFAObList(const CDFAOblist<T>& toCopy)
    

    【讨论】:

    • 谢谢!这几乎解决了我的问题,我只需要将 toCopy 上的 -&gt; 运算符也更改为 . 运算符 :)
    猜你喜欢
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 2015-02-01
    相关资源
    最近更新 更多