C++模板类专业化和结构
我花了几个小时在网上搜索解决方案,但无济于事。我正在 Xcode 中编写 C++
#import "data.h" // contains a struct called data
template <class T>
class container {
public:
container();
~container();
private:
// functionality for containing T
};
template <class T>
container<T>::container() { /* generic */ }
template <class T>
container<T>::~container() { /* generic */ }
template <>
container<data>::container() { /* template specialization of data */ }
编译器抱怨:重复符号并指出类模板专门化。我认为也许是因为构造无法专门化,所以我尝试了添加额外的 void 函数的方法
template <class T>
class container {
public:
container();
~container();
void setup();
private:
// functionality for containing T
};
template <>
void container<data>::setup() { /* template specialization of data */ }
,但这给了我相同的编译器错误。我现在真的不知道在哪里寻找解决方案......
I've spent hours searching on the web for a solution but to no avail. I'm programming C++ in Xcode
#import "data.h" // contains a struct called data
template <class T>
class container {
public:
container();
~container();
private:
// functionality for containing T
};
template <class T>
container<T>::container() { /* generic */ }
template <class T>
container<T>::~container() { /* generic */ }
template <>
container<data>::container() { /* template specialization of data */ }
The compiler complains: duplicate symbol and points out the class template specialization. I thought that maybe it was because of constructs not being able to specialize, so I tried something along the lines of adding an extra void function
template <class T>
class container {
public:
container();
~container();
void setup();
private:
// functionality for containing T
};
template <>
void container<data>::setup() { /* template specialization of data */ }
But this gives me the same compiler error. I don't really have any idea of where to look for a solution now...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您专门化一个类模板时,您必须专门化所有成员函数。
除了设置之外,您还需要专门化构造函数/析构函数。
When you specialize a class template, you must specialize ALL of the member functions.
In addition to setup, you still need to specialize the constructor/destructor.