我正在使用模板类(Pol<T>
)以多项式进行计算并希望使用成员函数(.exp()
)将多项式P转换为其指数Ë^ P。
重载指数函数工作正常,编译器选择原来的指数exp(double)
如果T = double
和我自己,如果T=Pol<double>
,但在成员函数,我得到:如何使用模板类成员中的超载和原始EXP
error: no matching function for call to ‘Pol<double>::exp(double&)’
我不能使用std :: EXP的成员函数,因为我使用的多项式的多个订单,如:
Pol< Pol< complex<double> > > P1
我可以使用重载的指数做一个解决办法,但我不明白,为什么它不应该成员内部是可能的。
这里是我的代码:
#include <iostream>
#include <math.h>
#include <vector>
using std::cout;
using std::endl;
template < class T>
class Pol;
template < class T >
const Pol<T> exp(const Pol<T>& P);
template < class T >
class Pol{
protected:
std::vector<T> Data;
public:
inline Pol():Data(1){}
inline const T operator[](int i)const{return Data[i];}
inline T& operator[](int i){return Data[i];}
Pol& exp();
};
template < class T >
const Pol<T> exp(const Pol<T>& P){
Pol<T> Erg(P);
Erg[0] = exp(P[0]); // works fine
return Erg;
}
template < class T >
Pol<T>& Pol<T>::exp(){
Data[0] = exp(Data[0]); // here appears the error
return *this;
}
int main() {
Pol<double> P1;
P1 = exp(P1); // this works
P1.exp(); // this enforces the error
cout << "P1[0]" << P1[0] << endl;
return 0;
}
有一个' ;''之前'''。这可能是你的问题。 –
另外'模板< class T > Pol;'缺少'class'关键字。这是你的真实代码吗? :/ – jrok
修复语法错误之后,它[编译正常](http://coliru.stacked-crooked.com/a/2d6a4aaa8ac226f0)。投票结束。 – jrok