0

pls have a look at this code:

template<typename T>
class myclass {
private:

  class node {
    friend class myclass;

    T t;

    node(T t_) : t(t_) {};
  };

  node nd;

  node getNode();

public:
  myclass(T t_) : nd(node(t_)) {};

  T get() {
    auto node = getNode();
    return node.t;
  }

};

template<typename T>
myclass<T>::node myclass<T>::getNode() {
  return nd;
}

I define the method getNode() outside the template, and the compiler report this error:

missing 'typename' prior to dependent type name 'myclass<T>::node' myclass<T>::node myclass<T>::getNode() {

how should I write it correctly?

Ziqi Liu
  • 2,037
  • 1
  • 18
  • 41
  • just to add a bit to the given answer... when you write `myclass::node` it might be that after that you will add a specialisation of `myclass` where `node` is not a type but a member, hence compiler needs your help to be certain that `node` is a type, not something else – 463035818_is_not_a_number Aug 01 '18 at 09:34

1 Answers1

4

Need to use typename keyword for dependent type myclass<T>

template<typename T>
typename  myclass<T>::node myclass<T>::getNode() {
~~~~~~~~
  return nd;
}
P0W
  • 42,046
  • 8
  • 62
  • 107
  • why I should add `typename`? Generally when I define member function outside the template class I don't have to add that.... – Ziqi Liu Aug 02 '18 at 02:25
  • @ZiqiLiu check [this](https://stackoverflow.com/q/610245/1870232) post to understand why. – P0W Aug 02 '18 at 05:11