Strange problem with classes

Just had a quick question, with the following code im getting the error
in function 'polynomial operator+(const polynomial&, const polynomial&)'
passing 'const polynomial as 'this argument of 'const double polynmial::coefficient(unsigned int)'discards qualifyer
thank you in advance for any help.
-ed

polynomial operator +(const polynomial &p1, const polynomial &p2)
{
polynomial p3;
for (unsigned int i = 0; i <= MAX_EX; i++)
{
p3.assign_coef(p1.coefficient(i) + p2.coefficient(i), i);

}
return p3;
}

const double polynomial::coefficient(unsigned int exponent)
{
return polyHolder[exponent];
}

void polynomial::add_to_coef(double amount, unsigned int exponent)
{
polyHolder[exponent] += amount;

}

Comments

  • I think the compiler is moaning because you are calling a non-constant method of a constant object.

    Instead of

    [b]const[/b] double polynomial::coefficient(unsigned int exponent)

    try

    double polynomial::coefficient(unsigned int exponent) [b]const[/b]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion