I have a member function:
Code:
T& at(int n)
{
return coeff.at(n);
}
where coeff is a std::vector<T>
When I attempt to use the returned value in a multiplication with another T instantiated to integers, I get the following error:
Code:
polynomial.hpp:76:4: error: invalid operands of types ‘int(int (*)())’ and ‘int’ to binary ‘operator*’
Here is the complete code:
polynomial.hpp
Code:
#ifndef MM_POLY
#define MM_POLY
#include<vector>
#include<string>
#include<sstream>
#include<initializer_list>
#include<stdexcept>
namespace mm
{
template<typename T=double>
class Polynomial
{
public:
Polynomial(std::initializer_list<T> dat)
{
for(auto x:dat)
coeff.push_back(x);
}
Polynomial(int n)
{
coeff.reserve(n+1);
}
std::string str(std::string var) const
{
std::ostringstream os;
for(uint i=0;i<coeff.size()-1;++i)
os<<coeff[i]<<"*"<<var<<"^"<<i<<" + ";
os<<coeff[coeff.size()-1]<<"*"<<var<<"^"<<coeff.size()-1;
return os.str();
}
T eval(T t) const
{
T result = T(0);
T mul = T(1);
for(int i=0;i<=degree();++i,mul*=t)
result+=mul*coeff[i];
return result;
}
int degree() const
{
return coeff.size()-1;
}
T& at(int n)
{
return coeff.at(n);
}
private:
std::vector<T> coeff;
};
template
<
typename T
>
std::pair
<
Polynomial<T>
,T
>
SyntheticDivision(Polynomial<T> dividend,Polynomial<T> divisor)
{
int qdeg = dividend.degree()-1;
Polynomial<T> quotient(qdeg);
T remainder(T());
if(divisor.degree()!=1)
throw(std::logic_error("SyntheticDivision:Divisor.Degree !=1"));
T h=- divisor.at(0)/divisor.at(1);
for(int i=qdeg;i>=0;--i)
{
remainder = dividend.at(i+1) + remainder*h;
quotient.at(i)=remainder;
}
return std::pair<Polynomial<T>,T>(quotient,remainder);
}
}
#endif
Similar errors are reported at lines 76,77 and 80.
main.cpp
Code:
#include "polynomial.hpp"
#include<iostream>
int main()
{
mm::Polynomial<int> dn({2,-3,1}),dv({-1,1});
auto result = mm::SyntheticDivision<int>(dn,dv);
std::cout<<result.first.str("x");
}
I couldn't produce the problem with a simpler example:
Code:
template<typename T>
class Foo
{
public:
Foo(T t):foo(t){};
T& val()
{
return foo;
}
private:
T foo;
};
int main()
{
Foo<int> bar(6);
int xip = 5;
int bas = xip * bar.val();
}