【发布时间】:2020-11-14 00:20:10
【问题描述】:
类模板成员特化的问题。
我有一个简单的类,我想专门化一个成员函数。我是这样做的:
template <class T>
class Object
{
public:
Object() {}
void print() const
{
std::cout << "It's the template" << std::endl;
}
// ...
};
template<>
void Object<int>::print() const
{
std::cout << "It's an int" << std::endl;
}
对于成员函数的多重定义会导致编译错误。
如果只有一个源文件包含标题,一切都很好。
如果两个文件包含标题,我会收到以下错误:
/home/marc/QtProjects/QtAsp-Service/build-aspservice-Desktop_Qt_5_15_0_GCC_64bit-Debug/../src/Asp/aspobject.h:34: Fehler: 'Asp2::print()'的多重定义; main.o:/home/marc/QtProjects/QtAsp-Service/build-aspservice-Desktop_Qt_5_15_0_GCC_64bit-Debug/../src/Asp/aspobject.h:34:这里首先定义
这在一般情况下可能吗?如果是,有什么问题。
【问题讨论】:
标签: c++ templates template-specialization