Defining template class member functions in a namespace?

user6335594

I looked around and tried to find an answer to this. Is it possible to define the member functions of a template class in a namespace within a cpp file? I get an error when I try to do it.

Here are the two files I am trying to compile.

ArrayList.hpp

     template<typename T>
     class ArrayList{
          ArrayList();
          ~ArrayList();
     }

ArrayList.cpp

    #include "ArrayList.hpp"

    namespace{

        template<typename T>
        ArrayList<T>::ArrayList(){

         /* function body goes here */

        }

        ArrayList<T>::~ArrayList(){

         /* function body goes here */

        }

Compiler error

 error: cannot define or
  redeclare 'ArrayList<T>' here because namespace '' does not enclose
  namespace 'ArrayList'
  ArrayList<T>::ArrayList()
Shiro

You need to declare your class in the same namespace as you define its member functions.

And you are missing a template<typename T> before your destructor.

namespace ANamespace
{

    template<typename T>
    class ArrayList
    {
        ArrayList();
        ~ArrayList();
    };

    template<typename T>
    ArrayList<T>::ArrayList()
    {

        /* function body goes here */

    }

    template<typename T>
    ArrayList<T>::~ArrayList()
    {

        /* function body goes here */

    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Defining unnamed class member functions?

Variadic template functions in namespace and in class

defining inline functions under template class definition

how to specialize template member functions in template class?

C++ Functor template for class member functions

Correct template definition to wrap class member functions

Different template class realizations but same member functions

Explicit instantiation of template class with templated member functions

Cyclic dependency between template class member functions

Is it possible to use a class template in its member functions?

Overloading << and >> as outside member functions for template class

Passing a set of pointers to member functions of a template class

Defining a variable member of a namespace outside the scope of the namespace

Defining a Single Template for a Member Function within a Class Template with Both Templates Used in Member Function

"using namespace" for defining global functions

Defining functions from nameless namespace outside the namespace

How to specialize template member functions in template class (already specilzied)?

Namespace or class with one member?

Validity of style of defining class in namespace

Defining functions inside class

only calls to certain template class member functions work

C++ name resolution for member functions in template class

Automatic generate member functions depending on inherited class template

How to write a template wrapper method for other class member functions?

Template Specialization - member functions

Calling template member functions

template deduction of member functions

template class to class with template member

namespace specialization in template class