繁体   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