0

以下のコードで:

マテリアル.h:

#ifndef MATERIA_H
#define MATERIA_H

class material
{
public:
  template <class type>
  static material* MakeMaterial(typename type::configtype, long);
  template <class type>
  void CreateNaturalForm(typename type::configtype, long);
  … 
};

template <class type>
material* material::MakeMaterial(typename type::configtype Config, long Volume)
{
  return type::Spawn(Config, Volume);
}

#endif

マテリアル.h:

#ifndef MATERIAS_H
#define MATERIAS_H

#include "materia.h"
#include "confdef.h"

class solid : public material {
public:
  typedef solidmaterial configtype;
  … 
};

template material* material::MakeMaterial<solid>(solidmaterial, long);

template <class type>
void material::CreateNaturalForm(typename type::configtype Config, long Volume)
{
  … 
  MakeMaterial(Config, Volume); // Error here
  … 
}

template void material::CreateNaturalForm<solid>(solidmaterial, long);

#endif

confdef.h:

#ifndef CONFDEF_H
#define CONFDEF_H

enum solidmaterial {
  WOOD,
  … 
};

#endif

main.cpp

#include "materia.h"
#include "materias.h"
#include "confdef.h"

int main()
{
  material::MakeMaterial(WOOD, 500); // Same error here
}

(エラーを再現する上記のコードのオンライン バージョンは次のとおりです。)

コメント行に次のコンパイル エラー メッセージが表示されます。

「MakeMaterial」の呼び出しに一致する関数がありません

私は何を間違っていますか?明示的なインスタンス化により、コンパイラが正しい関数を認識できるようになるべきではありませんか?

明示的に記述すればコードはコンパイルされますが、ここでの要点は引数からMakeMaterial<solid>推測することです。どうすればこれを達成できますか?typeConfig

4

1 に答える 1

2

通話中

MakeMaterial(Config, Volume); // Error here

type::configtypeコンパイラは、関数テンプレート内の が の型である一致を見つけるように求められますConfig

しかし、何に一致typeするかをコンパイラに伝えるものは何もありません。これは明示的なインスタンス化ではありません。

type一般に、 に一致する可能性のある数百のタイプが存在する可能性がありtype::configtypeますConfig。C++ は、可能な型が 1 つしかない特殊なケースをサポートしていません。

それを修正する方法は、何を達成するつもりだったかによって異なります。

于 2015-06-29T18:35:54.487 に答える