C++正向声明 - 不允许不完整的类型

发布于 2025-02-10 23:22:23 字数 1603 浏览 1 评论 0原文

我有多个课程,并按顺序写了它们。头等舱有枚举,但我想将其移至所有其他班级以下的课程。这是问题开始的地方。由于最后一类将拥有该枚举,因此在头等舱中使用ENUM的成员会产生错误。

这是初始代码:

file.hpp:

class A
{
public:
    typedef enum
    {
        eOk = 0x00;
        eError = 0x01;
    }myEnum_et;

    typedef myEnum_et(*callbackType)(B*);
    callbackType myCallback;

public:
    void foo1(callbackType callbackParam)
    {
        myCallback = callbackParam;
    }

    myEnum_et foo2()
    {
        return myCallback;
    }
};

class B
{
public:

    A::myEnum_et foo3(A *a)
    {
        if(a->foo2() == NULL) return A::eOk;
        else return A::eError;
    }
};

class C
{
public:
    B *b;

    void foo4(B *new_b)
    {
        b = new_b;
    }    
};

我想做的是:

class C;
class B;

typedef C::myEnum_et(*callbackType)(B*);

class A
{
public:
    callbackType myCallback;

public:
    void foo1(callbackType callbackParam)
    {
        myCallback = callbackParam;
    }

    myEnum_et foo2()
    {
        return myCallback;
    }
};

class B
{
public:

    C::myEnum_et foo3(A *a)
    {
        if(a->foo2() == NULL) return C::eOk;
        else return C::eError;
    }
};
    
class C
{
public:
    typedef enum
    {
        eOk = 0x00;
        eError = 0x01;
    }myEnum_et;

    B *b;

    void foo4(B *new_b)
    {
        b = new_b;
    }    
};
  

如上所述,我只想将myenum_et从A类移动到C类,然后将功能指针移至A级A级范围。如您所知,对于使用myenum_et的人来说,在代码顶部宣布B类是不够的。我想原因是编译器需要对B类的完整定义。这是错误:

#71不完整类型。

我知道代码的可读性很丑陋。我只是想证明所有类的定义彼此都取决于彼此,并希望在此类顺序上使用远期声明(或其他解决方案)。顺便说一句,我使用C ++ 03,所以我有点有限。

I have multiple classes and have written them in an order. First class has an enum, but I want to move it to the class that is below all other classes. This is where problem begins. Since the last class will have that enum, members that use enum in first class gives error.

Here is the initial code:

file.hpp:

class A
{
public:
    typedef enum
    {
        eOk = 0x00;
        eError = 0x01;
    }myEnum_et;

    typedef myEnum_et(*callbackType)(B*);
    callbackType myCallback;

public:
    void foo1(callbackType callbackParam)
    {
        myCallback = callbackParam;
    }

    myEnum_et foo2()
    {
        return myCallback;
    }
};

class B
{
public:

    A::myEnum_et foo3(A *a)
    {
        if(a->foo2() == NULL) return A::eOk;
        else return A::eError;
    }
};

class C
{
public:
    B *b;

    void foo4(B *new_b)
    {
        b = new_b;
    }    
};

What I want to do is:

class C;
class B;

typedef C::myEnum_et(*callbackType)(B*);

class A
{
public:
    callbackType myCallback;

public:
    void foo1(callbackType callbackParam)
    {
        myCallback = callbackParam;
    }

    myEnum_et foo2()
    {
        return myCallback;
    }
};

class B
{
public:

    C::myEnum_et foo3(A *a)
    {
        if(a->foo2() == NULL) return C::eOk;
        else return C::eError;
    }
};
    
class C
{
public:
    typedef enum
    {
        eOk = 0x00;
        eError = 0x01;
    }myEnum_et;

    B *b;

    void foo4(B *new_b)
    {
        b = new_b;
    }    
};
  

As seen above, I just want to move myEnum_et from class A to class C and move function pointer to out of class A's scope. As you know, declaring class B at the top of code is not enough for those that will use myEnum_et. I guess the reason is that compiler needs full definition of class B. Here is the error :

#71 Incomplete type is not allowed.

I am aware that code's readability is ugly. I just wanted show that all classes' definition depend on each other and want to use forward declaration(or other solutions) at this class order. By the way, I use C++03 so I am kinda limited.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文