模板类的特化成员-不匹配-数组



我有一个模板类,实现函数:

template<typename T>
class Matrix
{
...
void setItems(const T *tab)
{
    //writing content from tab to Matrix internal data 
}
...
};

一切都很好,直到我想为char*创建专门的函数,我的类必须为string分配内存等等。我想用:

template<> void Matrix<char*>::setItems(const char** tab)
{
...

问题是,这不会生成:

template-id 'setItems<>' for 'void Matrix<char*>::setItems(const char**)' does not match any template declaration

到目前为止,我对专门的函数没有任何问题。我错过了什么?


额外的信息:我必须使用char*

如果T是char *,则const T *是char *const *。

所以你的成员函数应该是:

template<> void Matrix<char*>::setItems(char*const* tab)
{
  ...
}
在 类型后面加上const是相当常见的。
void setItems(T const* tab)

在你的例子中,它使扩展类型更明显

最新更新