3

I have the following code in the private section of a basic Polynomial class .h file:

private:
struct Term
{
    float coefficient;
    int power; // power of specific term
};
int degree; //total number of terms

I have the following default constructor for the Polynomial class:

Polynomial::Polynomial()
{
    Polynomial.Term poly;
    poly.power = 0;
    poly.coefficient = 0;
    degree = 1;
}

I am confused on how to access the terms inside the struct as well as the variable outside the struct. I tried to google this, but couldn't find anything helpful.

Edit: overloaded output operator code

ostream & operator << (ostream & outs, Polynomial & P)
{
    outs << P[0].poly.coefficient << "x^" << P[0].poly.power;
    for (int i=1; i<P.degree-1; i++)
    {
        if (P[i].poly.coefficient > 0)
            outs << "+";
        outs << P[i].poly.coefficient << "x^" << P[i].poly.power;
    }
    outs << endl;
}
5
  • Where did you get the poly. from? Commented Nov 21, 2017 at 4:21
  • that is a variable I initialized using Term, trying to access the variables in Term. Commented Nov 21, 2017 at 4:23
  • Can you post the rest of your code? Commented Nov 21, 2017 at 4:23
  • The rest of the code isn't really relevant. I am just trying to figure out how to access the terms in the Term struct in the default constructor. What code are you asking for? Commented Nov 21, 2017 at 4:25
  • 1
    Polynomial.Term Did this really compile? Commented Mar 20, 2018 at 13:46

1 Answer 1

5

You've declared the variable within the constructor function. Once execution leaves the constructor, that variable is destroyed. Instead, you need to add the variable to the class declaration. If you want to be able to access the members from outside any of the class functions, you'll also need to make the variables and struct definition public.

class Polynomial
{
public:
   struct Term
   {
      float coefficient;
      int power; // power of specific term
   };
   Term poly;
   int degree; //total number of terms

   Polynomial();
};

Polynomial::Polynomial()
{
    poly.power = 0;
    poly.coefficient = 0;
    degree = 1;
}
Sign up to request clarification or add additional context in comments.

4 Comments

thank you very much. Now when overloading the output operator, I'm having trouble accessing power and coefficient. code updated in post
@JulieRosen - That sounds like a different problem. You should post a new question about that.
@JulieRosen Yeah, that's a different issue, but as a quick hint: Your "P" variable in that function isn't an array. It looks like what you want is for the "poly" variable to be an array. e.g. Term poly[100]; Then you need to index 'poly', e.g. P.poly[i]
Bonus tip, an "std::vector<Term>" is probably more appropriate for this data, rather than using "degree".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.