2

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()
  • namespace should be named, i.e. : `namespace myarray {` – Serge Jun 30 '17 at 02:27
  • @Serge you can have anonymous namespaces in C++ also – Curious Jun 30 '17 at 02:29
  • @Serge Leads to a similar error -> 'cannot define or redeclare 'ArrayList' here because namespace 'myarray' does not enclose namespace 'ArrayList' ArrayList::ArrayList()' –  Jun 30 '17 at 02:30
  • @user87002 please post your new code in your question as an edit, including the namespace definitions in both the header file and the implementation file – Curious Jun 30 '17 at 02:30
  • check your code for unballanced braces. You are missing the closing one in the namespace example. – Serge Jun 30 '17 at 02:36

3 Answers3

3

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 */

    }
}
Shiro
  • 2,610
  • 2
  • 20
  • 36
1

Is it possible to define the member functions of a template class in a namespace within a cpp file?

No, that's not possible. As with any other class declarations the member function declarations and their definitions must appear in the same namespace.

Also unnamed namespaces as used in your example will be seen only in that particular translation unit.

You generally can't put template definitions to separate translation units (unless you have particular specialization cases). Read more about this here please:

Why can templates only be implemented in the header file?


How to fix it?

Just move everything out to your header file and drop the unnamed namespace like this:

ArrayList.hpp

 template<typename T>
 class ArrayList{
      ArrayList();
      ~ArrayList();
 }; // Semicolon at end of class declaration is essential

 template<typename T>
 ArrayList<T>::ArrayList(){
     /* function body goes here */
 }

 template<typename T> // <<<<< Repeat for evey function definition
 ArrayList<T>::~ArrayList(){
    /* function body goes here */
 }

If you really want to have a namespace, just provide a named one and enclose everything from above (still in the header):

 namespace MyUtilities {
     template<typename T>
     class ArrayList{
     public: // <<<<<< Allow access to these functions
          ArrayList();
          ~ArrayList();
     };

     template<typename T>
     ArrayList<T>::ArrayList(){
          /* function body goes here */
     }

     template<typename T>
     ArrayList<T>::~ArrayList(){
         /* function body goes here */
     }
 }

See the fully working example here please.

Also to make that stuff usable use the public keyword as shown above.

0

Firstly you cannot define a template class's methods in a .cpp file. You need to define template class methods in the header file itself (or in another file and then import that via preprocessor includes in the header file itself) For more see Why can templates only be implemented in the header file?

Anonymous namespaces namespace { are used to define things that have internal linkage, i.e. similar to the effect static has on functions defined in implementation files. Read this for more information Why are unnamed namespaces used and what are their benefits?

But in your case you want to define the class methods to be externally visible because you have them declared publicly in the header file. So you probably don't want to have those definitions be in an anonymous namespace.

If you have a namespace enclosing your class in your header file, for example namespace array_list then you should just define your class methods in that same namespace in the header file itself since it's a template class and needs to be visible wherever used.

Curious
  • 20,870
  • 8
  • 61
  • 146