Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板专业化,专业化类问题_C++ - Fatal编程技术网

C++ 模板专业化,专业化类问题

C++ 模板专业化,专业化类问题,c++,C++,我有下面的代码,当我在A部分编译时,函数与模板专门化不匹配。有人知道这是怎么回事吗 我在#ifdef代码中定义了一个专门的类 谢谢 #include <iostream> using namespace std; template<class T1, class T2, int I> class A { public: void f0(); }; template<class T1, class T2, int I> voi

我有下面的代码,当我在A部分编译时,函数与模板专门化不匹配。有人知道这是怎么回事吗

我在#ifdef代码中定义了一个专门的类

谢谢

#include <iostream>
using namespace std;

template<class T1, class T2, int I>
class A {
public:
    void f0();
};            

template<class T1, class T2, int I>
void A<T1, T2, I>::f0()
{
    cout << "x" << endl;
}

#if 0 // A
template<>
class A<int, int, 4> {
public:
    void f0();
};
#endif

template<>
void A<int, int, 4>::f0()
{
    cout << "z" << endl;
}

int main(int argc, char *argv[])
{
    A<int, int, 4> a0;
    a0.f0();
    return 0;
}
#包括
使用名称空间std;
模板
甲级{
公众:
void f0();
};            
模板
void A::f0()
{

cout您需要编写没有“模板”的成员函数,如下面所示:

template<>
class A<int, int, 4> {
public:
    void f0();
};

void A<int, int, 4>::f0()
{
    cout << "z" << endl;
}
模板
甲级{
公众:
void f0();
};
void A::f0()
{

难道我不明白吗?定义类专门化时代码不起作用?或者相反?编译#if def'd部分时代码不起作用。按原样,它起作用。你知道为什么第二个模板会导致问题吗?我在本文中看不到这一点document@notaorb实际上我不确定…我猜A班的特殊化“模板”是在声明A.@notaorb时完成的,因为
A::foo()
不是一个
模板
,它是一个完全专用类模板上的函数。类
a
的定义需要
模板
,因为这是专门化模板
a
——但所有其他对该类型的引用,或与该类型相关的内容,实际上都是“具体的”“类型,就像其他类型一样class@Human-编译器谢谢,但我认为发布的编译代码不是这样。
template void A::f0()
是完全专用类模板上的函数,但是删除
模板会导致编译错误。
template<>
class A<int, int, 4> {
public:
    void f0();
};

void A<int, int, 4>::f0()
{
    cout << "z" << endl;
}