templates and inheritance

Hi. The following code cannot compile - use of undeclared identifier. I use GCC and XCode for compilation.

Everything is in a single header file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
include "MyArray.h"

template <typename T>
class MyBase {
public:
  MyBase();
  virtual ~MyBase();
  void addStuff(T* someStuff);
protected:
  MyArray<T*> stuff;
};

template <typename T>
MyBase<T>::MyBase() {}
template <typename T>
MyBase<T>::~MyBase() {}

template <typename T>
void MyBase<T>::addStuff(T* someStuff) {
  stuff.add(someStuff);
}

// ---------------------

template <typename T>
class MyDerived : public MyBase<T> {
public:
  MyDerived();
  virtual ~MyDerived();
  virtual void doSomething();
};

template <typename T>
MyDerived<T>::MyDerived() {}
template <typename T>
MyDerived<T>::~MyDerived() {}

template <typename T>
void MyDerived<T>::doSomething() {
  T* thingy = new T();
  addStuff(thingy); //here's the compile error. addStuff is not declared.
}


Does anyone have an explanation?
Thanks in advance!
Last edited on
The semicolon is missing from the end of the class definitions at line line 11 and 31.

The return type is missing from line 19, 30 and 39.

Last edited on
Yes, sorry, I corrected this. But that's not the problem, you know...
Try this->addStuff(thingy);
exactly, this-> is the solution ;) thanks
If you're curious why this works, see C++ FAQ at http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19
Topic archived. No new replies allowed.