![](/img/trans.png)
[英]How to use doxygenclass directive for overloaded class template
[英]How to use the overloaded and original exp inside a member of a template class
我使用模板類( Pol<T>
)來計算多項式,並希望使用成員函數( .exp()
)將多項式P轉換為其指數e ^ P.
重載指數函數工作正常,編譯器選擇原始指數exp(double)
如果T = double
,我自己如果T=Pol<double>
,但在成員函數中得到:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.