次のコードはMSVCではうまくコンパイルされますが、gcc/Xcodeではchockesがコンパイルされます。 test1.hでこのコードをコンパイルできるのはMSVCだけ
:test2.hで
template<class T, class T2>
#ifdef WIN32
class __declspec(dllexport) Base
#else
class Base
#endif
{
protected:
struct Impl;
Impl *pimpl;
virtual int Baz(std::vector<T2> &errorMsg) = 0;
public:
virtual ~Base() = 0;
Impl &GetTableVector() { return *pimpl; };
virtual int Foo(T *param, std::vector<T2> &errorMsg) = 0;
virtual int Bar(std::vector<T2> &errorMsg) = 0;
};
template<class T, class T2> struct Base<T, T2>::Impl
{
std::vector<Table> m_tables;
};
template<class T, class T2> Base<T, T2>::~Base()
{
delete pimpl;
}
:test2.cppで
template <class T, class T2>
#ifdef WIN32
class __declspec(dllexport) Derived : public Base<T, T2>
#else
class Derived : public Base<T, T2>
#endif
{
public:
Derived();
virtual ~Derived();
virtual int Foo(T *param, std::vector<T2> &errorMsg);
virtual int Bar(std::vector<T2> &errorMsg);
protected:
void GetErrorMessage(int code, T2 &errorMsg);
virtual int Baz(std::vector<T2> &errorMsg);
};
:
template<class T, class T2> Derived<T, T2>::Derived() : Base<T, T2>()
{
pimpl = new Impl;
}
エラーメッセージがある:
error: pimpl was not declared in this scope.
この変数は、基本クラスでprotectedと宣言されているため、派生クラスから参照できるようにする必要があります。
奇妙なことは、そのコードはテンプレートを使用せずにうまくコンパイルしていたことです。
どうすればいいですか?
Thx。
あなたの 'Derived'コンストラクタは、おそらく' Base'コンストラクタを呼び出すことを意図して、それ自身に委譲します。 – user657267
'this-> pimpl'を試してください。 –
@ user657267、修正されました。ちょうどタイプミス、申し訳ありません。 – Igor