尝试在显式初始化期间创建函数副本时出错

pro*_*eve 2 c++ generics templates

我无法理解我在模板类程序中遇到的错误.

#include <iostream>
#include<string>

using namespace std;

template <class dataType> class myClass {
public:
void function();
};

template<> class myClass<int> {
public:
void expli_function();
};

template <class dataType> void myClass<dataType>::function() {
cout << "Not an explicit function !" << endl;
}

template <class int> void myClass<int>::expli_function() { //<-- while pointing towards errors compiler points here
cout << "Explicit function !" << endl;
}

int main() {
myClass<string> ob1;
myClass<int> ob2;
ob1.function();
ob2.expli_function();
} 
Run Code Online (Sandbox Code Playgroud)

错误是:

tester_1.cpp(20): error C2332: 'class' : missing tag name
tester_1.cpp(20): error C2628: '<unnamed-tag>' followed by 'int' is illegal (did you forget a ';'?)
tester_1.cpp(20): error C2993: '' : illegal type for non-type template parameter '<unnamed-tag>'
error C2244: 'myClass<int>::expli_function' : unable to match function definition to an existing declaration
Run Code Online (Sandbox Code Playgroud)

为什么我会得到这些错误,我该如何解决?

BЈо*_*вић 5

这解决了它:

void myClass<int>::expli_function() {
    cout << "Explicit function !" << endl;
}
Run Code Online (Sandbox Code Playgroud)

由于class myClass<int>是特化,因此template<int>在定义函数方法之前不需要关键字.