クラスBのメンバーを持つクラスAをコンパイルしようとしています。ここで、クラスBにはデフォルトのコンストラクターがなく、その唯一のコンストラクターには複数の引数が必要です。簡単ですよね?どうやらそうではありません...
クラスA:
class SessionMediator
{
public:
SessionMediator()
: map_(16,100,100)
{}
Tilemap map_, background_, foreground_;
};
クラスB:
struct Tile2D;
class Tilemap
{
public:
Tilemap(const unsigned int tile_size, const unsigned int width,
const unsigned int height)
: tiles_(NULL), tile_size_(tile_size)
{
Resize(width, height);
}
inline void Resize(const unsigned int width, const unsigned int height)
{ /* Allocate tiles & assign to width_, height_... */ }
unsigned int tile_size_, width_, height_;
Tile2D* tiles_;
};
私は次のようにSessionMediatorをインスタンス化しています:
int main(int argc, char** argv)
{
SessionMediator session;
return 0;
}
これは私が得ているエラーです。Mac OS 10.5.8でXCodeをコンパイルしていますが、コンパイラはg++です。
session_mediator.h: In constructor 'SessionMediator::SessionMediator()':
session_mediator.h:19: error: no matching function for call to 'Tilemap::Tilemap()'
tilemap.h:31: note: candidates are: Tilemap::Tilemap(unsigned int, unsigned int, unsigned int)
tilemap.h:26: note: Tilemap::Tilemap(const Tilemap&)
session_mediator.h:19: error: no matching function for call to 'Tilemap::Tilemap()'
tilemap.h:31: note: candidates are: Tilemap::Tilemap(unsigned int, unsigned int, unsigned int)
tilemap.h:26: note: Tilemap::Tilemap(const Tilemap&)
(Duplicate of above here)
Build failed (2 errors)
私は基本的に同じことを行う短いコンパイル可能な例を書き、私が間違っていることを正確に理解しようとしました。これは、g++でエラーなしで問題なくコンパイルされます。
class A
{
public:
A(int x, int y, int z)
: x_(x), y_(y), z_(z)
{}
int x_, y_, z_;
};
class B
{
public:
B()
: m_a(1,2,3)
{}
A m_a;
};
int main(int argc, char **argv)
{
B test;
return 0;
}
最初の例で失敗するのはなぜですか?Tilemapの3argコンストラクター(Ex#1)は、Aの3 argコンストラクター(Ex#2)が呼び出されるのと同じ方法で呼び出されます。
コードは、2つの例の私とほとんど同じように見えます。