steve yee wrote:
g++ can't compile this code:
#include <iostream>
using namespace std;
template <typename T>
class Outer
{
public:
class Base
{
protected:
int m_n;
};
class Derived : public Base
{
public:
Derived(int n)
{
m_n = n; // g++ says m_n undeclared
}
void print()
{
cout << m_n << endl;
}
};
};
int main(int argc, char *argv[])
{
Outer<int>::Derived(100).print();
return 0;
}
You can't ask a compiler to guess what its suppose to do with that
template parameter. At the very least, a templated class will require a
ctor. There is also the fundamental question of what that "Outer" class
is suppose to bring to this design.
Maybe i'm wrong but you are attempting to solve a technical issue when
what i see is lack of a more fundamental startegy.
#include <iostream>
namespace Outer
{
template< typename T >
class Base
{
T m_t;
public:
Base() : m_t(0) { }
Base(T t) : m_t(t) { }
~Base() { }
T get() const { return m_t; }
};
template< typename T >
class Derived : public Base< T >
{
public:
Derived() { }
Derived(T t) : Base< T >(t) { }
~Derived() { }
void display() const { std::cout << Base< T >::get() <<
std::endl; }
};
} // namespace
int main()
{
Outer::Derived< int d0;
Outer::Derived< int d1(100);
d0.display();
d1.display();
return 0;
}