#include <stdio.h>
template<typename T, int N>
class A
{
public:
void func();
};
template<typename T, int N>
void A<int, N>::func()
{
printf("%d\n", N);
}
int main()
{
A<int, 3> a;
a.func();
return 0;
}
当我尝试编译这段代码,G ++提供了这些错误:如何从专门的成员函数访问模板参数?
test.cpp:10:22: error: invalid use of incomplete type ‘class A<int, N>’
test.cpp:4:7: error: declaration of ‘class A<int, N>’
我只设法编译这个时候A :: func被不专业的一切,当函数也是专门为N.
我如何专注于A :: func的T和访问N(应该可以是任何值)呢?
你确定这是相关的报价吗? – jrok
@jrok它是相关的,但它可能不是唯一相关的。我仍然在寻找一种明确表示无法部分专门化个别成员的方法。 (虽然可能没有这样的说法。) – jogojapan
OP并不是专门化成员,'func'不是模板。这里的问题是,在OP试图定义该特化的'func'成员的时候没有定义类模板部分特化。如果我们定义部分专业化,那么[It works](http://coliru.stacked-crooked.com/view?id=2b153beb291c2859b6de86f971c64881-0be4386eb9b168939a6a7d681c02387e)。 – jrok