-1
template<class T>
class base
{
    class nestedClass
    {
       T pos;
    };
   class derivedClass:public base<T>::nestedClass
     {
       void fun(){
         pos = pos +5; // error: pos is not declared in this scope
        }
     };   

};
pppery
  • 3,731
  • 22
  • 33
  • 46
Sean
  • 4,267
  • 10
  • 36
  • 50
  • 2
    That's full of syntax errors. – sth Feb 02 '11 at 04:23
  • sorry, I just simplified from my code. – Sean Feb 02 '11 at 04:25
  • 3
    Please copy and paste your *real* code. You've obviously omitted a lot of code, and some of what you've left out may be important in determining exactly what's wrong. Create a minimal example that compiles (or that you think *should* compile) and still demonstrates the problem. – Rob Kennedy Feb 02 '11 at 04:25
  • possible duplicate of [C++ Inherited template classes don't have access to the base class](http://stackoverflow.com/questions/4810272/c-inherited-template-classes-dont-have-access-to-the-base-class) – Chris Lutz Feb 02 '11 at 04:27
  • Sean, thank you for pasting real code, but I said a *minimal* example. Theres *way* more code than should be necessary for reproducing the "not declared in this scope" error message. In fact, it doesn't look like this code even exhibits the same problem you started with. Please don't change the goal of the question after people have already started answering. It turns the old answers into nonsense. – Rob Kennedy Feb 02 '11 at 04:46

3 Answers3

3

C++03 [Section 14.6/8] says

When looking for the declaration of a name used in a template definition, the usual lookup rules (3.4.1, 3.4.2) are used for nondependent names. The lookup of names dependent on the template parameters is postponed until the actual template argument is known (14.6.2).

Section 14.6.2/3 says

In the definition of a class template or a member of a class template, if a base class of the class template depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member.

pos is an non dependent name so the base class is not examined.

So you have two options.

  • Use fully qualified name of Member i.e base<T>::nestedClass::pos
  • Use this->pos.
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
1

It has to be explicitly referenced as base::nestedClass::pos [or use a using statement]

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • see http://stackoverflow.com/questions/4810272/c-inherited-template-classes-dont-have-access-to-the-base-class/4810438#4810438 – Foo Bah Feb 02 '11 at 04:25
  • If it's a dupe, comment and people will vote to close (and possibly upvote your other answer). – Chris Lutz Feb 02 '11 at 04:32
1

You're not bringing pos into scope. You can either use this->pos += 5; or using nestedclass::pos.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252