Reputation: 6255
Following code compiles fine on MSVC but chockes on gcc/Xcode.
In test1.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;
}
In test2.h:
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);
};
In test2.cpp:
template<class T, class T2> Derived<T, T2>::Derived() : Base<T, T2>()
{
pimpl = new Impl;
}
The error message is:
error: pimpl was not declared in this scope.
Well, this variable is declared in the base class as protected and so should be visible to the derived class.
The weird thing is - that code was compiling fine without using templates.
Any idea how do I fix it?
Thx.
Upvotes: 0
Views: 66
Reputation: 896
In test2.cpp
use this instead:
template<class T, class T2> Derived<T, T2>::Derived() : Base<T, T2>()
{
Base<T, T2>::pimpl = new typename Base<T, T2>::Impl;
}
On Ubuntu 15.04 with gcc 4.9.2 I can confirm that it compiles.
Upvotes: 1