簡體   English   中英

如何在模板類的成員中使用重載和原始exp

[英]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;
}

編輯后,解決方案非常簡單。 如果您有成員函數,則查找將忽略全局模板函數。 您需要明確引用它:

Data[0] = ::exp(Data[0]);
//        ^^ global scope

實例

如果您希望編譯器同時查看兩者 ,您還可以使用:

using ::exp;
Data[0] = exp(Data[0]);

實例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM