不同命名空间中函数的专用化
Specialization of a function in different namespace
当我尝试构建代码时,我收到以下错误。
src/Test.cxx:29:错误:"模板 T "的专业化 com::check::one::test::getValue(const std::string&(' in different 命名空间 ./incl/Test.hxx:30:错误:来自 'template T com::check::one::Test::getValue(const std::string&(' src/Test.cxx:31:被早期的错误所迷惑,保释 外
头文件:
namespace com::check::one
{
class Test
{
public:
template<typename T>
T getValue(const std::string& var);
};
}
源文件:
using namespace com::check::one;
template<>
std::vector<std::string> Test::getValue(const std::string& var)
{
//statements
}
我正在使用正确的命名空间,并且还包含头文件。编译中没有问题。甚至我已经在源文件中定义了测试类的其他成员函数。这些功能没有问题。只有这个有模板的函数有问题。错误发生在生成过程中。谁能帮我解决这个问题?
using namespace com.check.one;
不是有效的语法。它应该是:
using namespace com::check::one;
更好的是,将定义包装到命名空间中:
namespace com::check::one
{
class Test
{
public:
template<typename T>
T getValue(const std::string& var);
};
}
namespace com::check::one
{
template<>
std::vector<std::string> Test::getValue(const std::string& var)
{
//statements
}
}
此外,您应该阅读为什么只能在头文件中实现模板。
我希望在 cpp 文件中出现这样的东西:
namespace com {
namespace check {
namespace one {
template<>
std::vector<std::string> Test::getValue(const std::string& var)
{
//statements
}
}
}
}
这:
using namespace com.check.one;
比起C++,更像Java。C++ - using namespace com::check::one;
.它用于使用命名空间,而不是用于定义其中的内容。
相关文章:
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- 具有常量引用参数的函数模板专用化
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- C++模板专用化 - 无法匹配函数定义
- 具有专用化的模板类中的可靠条件复制和移动构造函数
- 如何使专用函数模板成为某个类的朋友?
- 如何按类型为元组元素创建专用函数
- 无法访问专用函数
- 使用指向部分专用函数成员的指针自动填充向量
- 64 位 G++ 4.6.3 不会在专用函数模板中将长整型视为长整型,即使它们的大小相同。这是一个错误吗?
- 操作员过载的专用函数
- C++调用模板专用函数
- 模板类中专用函数的声明
- 使用专用函数对异构集合的 STL 算法
- C++专用函数-不在此范围内错误
- 为什么我不能专用函数模板?
- 用于专用函数参数的模板
- 代码重复和模板专用化(当专用函数具有不同的返回类型时)
- 具有专用函数的Pull-Apart函数类型
- 专用函数模板