类成员的转发类声明

时间:2014-07-14 19:45:18

标签: c++ class forward-declaration

我遇到了问题,因为我希望我的包含在头文件中,并将其粘贴在源代码中以减少包含标题。

虽然以下类使用的是goocanvas库,但问题在于简单的c ++规则......

以下类是使用另一个类(前向声明)的类

#include <goocanvasmm/canvas.h> // due to: table main widget
namespace roulette
{
    class surface; // forward declared classes
    class mesh;

    class table final : public Goocanvas::Canvas
    {
    public:
        table ();
    private:
        Glib::RefPtr<surface> m_surface;  // using forward declared classes
        Glib::RefPtr<mesh> m_mesh;
    };
}

现在这里是上述表格的入口点。类

#include "surface.hpp"  // here we include forward declared classes
#include "mesh.hpp" // same here
#include "table.hpp"    // and the header it self

namespace roulette
{
    table::table() :  // ERROR: ALLOCATION OF INCOMPLETE TYPE
        m_surface(Glib::RefPtr<surface>(new surface)),  
        m_mesh(Glib::RefPtr<mesh>(new mesh(9, 29, 0, 0, 26, 19)))
    {
             /* implementation */
    }
}

如何包含标题&#39;网格&#39;和&#39;表面&#39;在标题而不是源文件中一切都很好。 如何解决这个问题,所以我不必在头文件中包含标题?

编辑: 这里是&#39;网格&#39;的标题和&#39;表面&#39;

#include <goocanvasmm/table.h> // due to: inheritance

namespace roulette
{
    class mesh final : public Goocanvas::Table
    {
    public:
        mesh (short rows, short columns,
                double x = 0.0,
            double y = 0.0,
            double width = 0.0,
            double height = 0.0);
};

}

#include <string>   // due to: image path placeholder
#include <gdkmm/pixbuf.h>   // due to: image loading
#include <goocanvasmm/itemsimple.h> // due to: ItemSimple class inheritance

class surface final : public Goocanvas::ItemSimple
{
public:
    surface ();

    Glib::RefPtr<Gdk::Pixbuf> get_chip(short value);

private:
    // members
    std::string m_data_path;
    Glib::RefPtr<Gdk::Pixbuf> m_image;
    Glib::RefPtr<Gdk::Pixbuf> m_chip1, m_chip5, m_chip25, m_chip50, m_chip100;

    // methods
    void simple_paint_vfunc(const Cairo::RefPtr<Cairo::Context>& cr, const Goocanvas::Bounds& bounds) override;
};

1 个答案:

答案 0 :(得分:1)

看起来像许多其他智能指针一样,Glib::RefPtr需要一个完整的类型,而不仅仅是前向声明。所以我担心你需要在table标题中包含标题。或者你可以使用Pimpl成语 - 参见例如http://herbsutter.com/gotw/_100/