Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define operator[]() (array subscription) for template class outside of class definition

Tags:

c++

templates

I thought this would be easy, but it's not working the way I expected. What is the correct syntax here?

TemplateClass.h

template <typename T> 
class TemplateClass
{
  T & operator[](size_t n);

TemplateClass.cpp

#include "TemplateClass.h"

template <typename T>
T & TemplateClass::operator[](size_t n)
{
  // member declaration not found
}
like image 917
Walt Dizzy Records Avatar asked Feb 24 '14 19:02

Walt Dizzy Records


1 Answers

You need to provide the whole class name – including template arguments:

template <typename T>
T & TemplateClass<T>::operator[](size_t n)
{
  // ...
}

(Also note that the scope resolution operator is ::, not :.)

like image 165
Konrad Rudolph Avatar answered Oct 15 '22 21:10

Konrad Rudolph