Question

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;
}
Was it helpful?

Solution

After your edit, the solution is quite simple. If you have a member function, the lookup ignores the global template function. You need to reference it explicitly:

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

Live example

If you want the compiler to see both, you could also use:

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

Live example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top