1

如果我有一个名为 Draw 的 UML 类,并且它与自身有这样的关联:

http://i.imgur.com/yifQN0p.png

如何在同一个类中为此创建 getter 和 setter?这是我的镜头:

#ifndef CDRAWABLE_H
#define CDRAWABLE_H
#include <list>

class CDrawable {
public:
    virtual ~CDrawable();

    void SetParent(CDrawable *mParent);
    CDrawable *GetParent() const;

private:
    //! \brief Default Constructor (Disabled)
    CDrawable();
    //! \brief Copy Constructor (Disabled)
    CDrawable(const CActor &);
    //! \brief Assignment Operator (Disabled)
    CDrawable &operator=(const CDrawable &);

    //! The parent of the drawable
    CDrawable *mParent;

    //! List of children of the drawable
    std::list<CDrawable *> mChildren;

对于我的 .cpp:

#include "wx/prec.h"
#include "CDrawable.h"
#include <cmath>

using namespace std;


CDrawable::~CDrawable() 
{
}

void CDrawable::SetParent(CDrawable *mParent)
{
    this->mParent = mParent;
}

CDrawable *CDrawable::GetParent()
{
    return mParent;
}

我在代码中省略了一些函数以减少冗长。

编辑:这是我在编译时遇到的错误:

CDrawable.cpp:153:12: error: prototype for ‘CDrawable* CDrawable::GetParent()’ does not match any in class ‘CDrawable’
CDrawable.h:25:16: error: candidate is: CDrawable* CDrawable::GetParent() const
4

1 回答 1

0

您的 GetParent 实现不应该是这样的吗?您的与标题中的内容不匹配。

const CDrawable *CDrawable::GetParent() const
{
    return mParent;
}
于 2013-10-24T20:35:32.180 回答