1

I am using a template class (Pol<T>) to calculate with polynomials and want to use a member function (.exp()) to convert a polynomial P into its exponential e^P.
Overloading the exponential function works fine, the compiler chooses the original exponential exp(double) if T = double and my own if T=Pol<double>, but in the member function I get:

error: no matching function for call to ‘Pol<double>::exp(double&)’

I cannot use std::exp in the member function, since I am using multiple orders of polynomials like:

Pol< Pol< complex<double> > > P1

I could use the overloaded exponential to make a workaround, but I don't see, why it should not be possible inside the member.

Here is my code:

#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;
}
4

1 回答 1

3

编辑后,解决方案非常简单。如果您有成员函数,则查找会忽略全局模板函数。您需要明确引用它:

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

活生生的例子

如果您希望编译器同时看到两者,您还可以使用:

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

活生生的例子

于 2014-05-05T19:01:35.427 回答