1

私を夢中にさせた回覧インクルードについて質問があります。

main.cpp

#include "A.hpp"
#include "B.hpp"

int main()
{
    A a();
    B b();
    return 0;
}

A.hpp

#ifndef _CLASS_A
#define _CLASS_A

#include "B.hpp"
class A
{
    public: 
        B* b;
        struct A_t
        {
            int id;
        };
};
#endif

B.hpp

#ifndef _CLASS_B
#define _CLASS_B

#include "A.hpp"

class B
{
    class A;  //Ok, with that I can use the class A
    public: 
        int a;
        A* b;  // That work!
        A::A_t *aStruct; // Opss! that throw a compilation error.

};
#endif

問題は: ¿クラス B で A_t 構造体を使用するにはどうすればよいですか?

次のような前方宣言を追加しようとしました。

struct  A::A_t;

しかし、それは明らかに機能します。

4

1 に答える 1

3

A.hinclude を前方宣言に置き換えます。

#ifndef _CLASS_A
#define _CLASS_A
class B;
class A
{
    public: 
        B* b;
        struct A_t
        {
            int id;
        };
};
#endif

また、

A a();
B b();

クラスの 2 つのインスタンスは作成されませんが、それらは関数宣言です。あなたがしたい

A a;
B b;
于 2012-11-14T10:18:16.657 に答える