39

Here is code sample which reproduces my problem:

template <typename myType>
class Base {
public:
    Base() {}
    virtual ~Base() {}
protected:
    int myOption;
    virtual void set() = 0;
};

template <typename InterfaceType>
class ChildClass : public Base < std::vector<InterfaceType> >
{
public:
    ChildClass() {}
    virtual ~ChildClass() {}
 protected:
    virtual void set();
};

template <typename InterfaceType>
void ChildClass<InterfaceType>::set()
{
     myOption = 10;
}

My usage in main():

ChildClass<int> myObject;

I get the following error (gcc 4.4.3 on ubuntu):

‘myOption’ was not declared in this scope

If my ChildClass would be without new template parameter this would work fine, i.e.:

class ChildClass : public Base < std::vector<SomeConcreteType> >

Edit

I've managed to solve it, if my set method looks like:

Base<std::vector<InterfaceType> >::myOption = 10;

It works fine. Still though not sure why I need to specify all template parameters.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Tadzys
  • 1,044
  • 3
  • 16
  • 22

2 Answers2

65

myOption is not a dependent name, i.e. it doesn't depend on the template arguments explicitly so the compiler tries to look it up early. You must make it a dependent name:

template <typename InterfaceType>
void ChildClass<InterfaceType>::set()
{
     this->myOption = 10;
}

Now it depends on the type of this and thus on the template arguments. Therefore the compiler will bind it at the time of instantiation.

This is called Two-phase name lookup.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • 1
    +1, I knew it but still missed it. :) – iammilind Aug 16 '11 at 09:49
  • Might require another question, but is there a way to avoid having to refer to every base class variable by `this->myVar`? It makes the code ugly. – Joey Dumont Feb 28 '14 at 17:44
  • @JoeyDumont: I can't think of any. Defining a function named myOption() returning a reference to this->myOption will shorten this (no pun intended), but IMO will be even more ugly. Besides, I don't think `this->` is ugly at all. This is how you would write it in C, say. – Yakov Galka Feb 28 '14 at 18:07
  • @ybungalobill Fair enough, but my code mostly consists of equations, so having tons of this-> makes it a little less readable. It's not _that_ big of an issue, though, so I won't search any further. – Joey Dumont Mar 01 '14 at 20:37
  • 1
    It works without this in Visual Studio 2008, just myOption = 10; is OK. – Tomas Kubes Apr 07 '14 at 12:10
  • 1
    @qub1n: it is not OK. It is Visual Studio 2008 which is not OK if it compiles this code. The authority that does specify what is correct C++ is ISO WG21, and it says that this program is invalid (see other answers for quotes). – Yakov Galka Apr 07 '14 at 17:36
19

C++03 14.6.2 Dependent names

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.

The following code should work.

template <typename InterfaceType>
void ChildClass<InterfaceType>::set()
{
   Base<std::vector<InterfaceType> >::myOption = 10;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Eric Z
  • 14,327
  • 7
  • 45
  • 69