tags:

views:

71

answers:

1

Possible Duplicate:
problem with template inheritance

This code doesn't compile in GCC:

template <typename T>
struct Base
{
public:
 int x;
};

template <typename B>
struct Middle : public B
{
};

template <typename T>
struct Sub : public Middle<Base<T> >
{
public:
 void F()
 {
  x=1; // error: ‘x’ was not declared in this scope
 }
};

If either Base or Sub weren't template classes, it wouldn't complain. VC handles it.

Why?

+4  A: 

Use this->x = 1; to tell the compiler that x is a (template-) dependent name. Note: What GCC does ot fine according to the standard, MSVC is just a bit more tolerant.

Alexander Gessler