0

I'm coding a Polynom class in c++. In the following link, it is explained how to return several values using tuple : Returning multiple values from a C++ function

So I tried to code as indicated to return the quotient and the remainder of the polynomial division but it does not work.

Here is my division method :

tuple<Polynome, Polynome> division(Polynome& D)
{   
    Polynome Q;
    Polynome R(*this);
    Polynome nul;
    int delta(degre() - D.degre());
    float a;

    while (delta >= 0 && R != nul)
    {
        a = R.top() / D.top();
        Polynome nouveauPoly(a, delta);
        Q = Q + nouveauPoly;
        R = R - nouveauPoly * D;
        delta = R.degre() - D.degre();
    }
    return {Q, R};
}

Then, when I do as indicated in the link above and begin to code my own operator :

Polynome operator/(Polynome& D)
{
    auto [Q, R] = division(D);
}

I get an error telling me Q should be constant value. I don't understand why Q should be so, and where the error come from, especially regarding that this code comes from a top answer in stackoverflow. I tried to first declare the polynomes Q and R but it did not change anything.

Ultimately I found a way to achieve what I wanted using tuples, but I'm still wanting to know exactly why the indicated answer did not work for me.

Thank you very much

  • 1
    Could you add the info about which line the compiler is complaingin (or better still the full error message)? Also are copy/move constructors/assignment operators available for the `Polynome` class? Btw: `operator/` is non-void and therefore requires a `return` statement. Arguably the signature (assuming it's a member function of `Polynome` class) should be `Polynome operator(Polynome const& d) const;` which would require similar changes to the `division` function. – fabian Sep 02 '21 at 07:12
  • 1
    please provide a [mre] – Alan Birtles Sep 02 '21 at 07:24
  • BTW: English = polynomial, German = Polynom, French = polynome. – gnasher729 Sep 02 '21 at 07:25
  • can't reproduce: https://godbolt.org/z/v4s86x47z – Alan Birtles Sep 02 '21 at 07:28
  • At a guess, you don't have a C++17 or newer compiler – Caleth Sep 02 '21 at 07:35
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 05 '21 at 14:23
  • Alright thank you very much, sorry I thought the code was enough I'll be more carefull next time. Indeed it should be the compiler. Thanks again. – Geoffrey Laforest Sep 18 '21 at 12:38

1 Answers1

3

It might be that Structured Binding does not work with your compiler version (or the C++ version option). That requires C++17.

If you can't go for C++17, you might want to try out older ways to do this:

Instead of

auto [Q, R] = division(D);

use

Polynome Q, R;
std::tie(Q, R) = division(D);

(This requires #include <tuple>, but I assume that is included already anyway.)

Aziuth
  • 3,652
  • 3
  • 18
  • 36