したがって、2つのクラスがあり、クラスAとBと呼びましょう。これらのクラスの内部には、関数の引数として相互に参照があります。私が前方宣言しようとすると、次のように宣言します。
// A.h (Header guarded)
namespace ns {
class B { // Attempt to forward declare B
public:
int getRand();
};
class A {
public:
float a, b;
void aFunc(B &b);
};
}
// B.h (Header guarded)
namespace ns {
class A { // Attempt to forward declare A
public:
float a, b;
};
class B {
public:
void bFunc(A &a);
int getRand();
};
}
問題は、これを行うと'class' type redefinition
エラーが発生したということです。私は解決策を探していましたが、まだ解決策に到達していません。私はこれを正しくやっていますか?私はそうではないと思いますが、ここでどこで間違ったことをしたのか教えてもらえますか?
それらは前方宣言ではなく、間違いなく定義です。
あなたがすべき
// A.h (Header guarded)
namespace ns {
class B; // forward declare B
class A {
public:
// Some functions with B references as arguments
};
}
そして、それは同じですB.h
。
状況に応じて、宣言されたクラスをパラメーターとして転送するメンバー関数がいくつかあります。メンバー関数の宣言を.hファイルに残し、それらの定義を.cppファイルに指定できます。といった
// A.h (Header guarded)
namespace ns {
class B; // forward declare B
class A {
public:
float a, b;
void aFunc(B &b);
};
}
// B.h (Header guarded)
namespace ns {
class A; // forward declare A
class B {
public:
void bFunc(A &a);
int getRand();
};
}
// A.cpp
#include "A.h"
#include "B.h"
namespace ns {
void A::aFunc(B& b) { /* ... */ }
}
// B.cpp
#include "A.h"
#include "B.h"
namespace ns {
void B::bFunc(A& b) { /* ... */ }
int B::getRand() { /* ... */ }
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加