【发布时间】:2017-06-30 02:25:16
【问题描述】:
我环顾四周,试图找到答案。是否可以在 cpp 文件的命名空间中定义模板类的成员函数?尝试执行此操作时出现错误。
这是我要编译的两个文件。
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 */
}
编译器错误
error: cannot define or
redeclare 'ArrayList<T>' here because namespace '' does not enclose
namespace 'ArrayList'
ArrayList<T>::ArrayList()
【问题讨论】:
-
命名空间应该被命名,即:
namespace myarray { -
@Serge 你也可以在 C++ 中使用匿名命名空间
-
@Serge 导致类似的错误 -> 'cannot define or redeclare 'ArrayList
' here because namespace 'myarray' does not enclose namespace 'ArrayList' ArrayList ::ArrayList() ' -
@user87002 请将您的新代码作为编辑发布在您的问题中,包括头文件和实现文件中的命名空间定义
-
检查你的代码是否有不平衡的大括号。您错过了命名空间示例中的最后一个。
标签: c++