![func1 func1]()
请参阅以下代码。第一个MyClass 具有两个函数(func1和func2)。然后,我想在func1中为MyClass做一些特殊的事情,而不是func2。看来我必须再次输入func2的代码。我想知道是否有解决此问题的方法?谢谢#include <iostream>using namespace std;template <class T>class MyClass {public: void func1(){ cout<<"default: func1"<<endl; } void func2(){ cout<<"default: func2"<<endl; }private: T haha;};template <>class MyClass<double> {public: void func1(){ cout<<"special: func1"<<endl; }};int main(){ MyClass<int> intclass; intclass.func1(); intclass.func2(); MyClass<double> doubleclass; doubleclass.func1(); doubleclass.func2(); // error 'class MyClass<double>' has no member named 'func2' return 0;} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 无需为整个类(class)提供特化知识。您可以专门化该特定的成员函数:template <>void MyClass<double>::func1() { cout<<"special: func1"<<endl;}现场演示here。 (adsbygoogle = window.adsbygoogle || []).push({}); 10-04 15:02