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()
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.
Comments