examples for >>

[edit]
Oh, yeah, I remember why I lost the will to help people.

They post a question, get help, then go out of their way to delete their questions.
Good job, mikey boyo, you've gone and treated others like tools because you're a tool.

May you trip on a recruiter and break an if.
[/edit]

You need to declare the extraction operator as a friend to the class:

1
2
3
4
5
6
7
8
9
10
class Polynomial {
  ...
  friend std::istream& operator>>(std::istream&, Polynomial&);
};

...

std::istream& operator>>(std::istream& ins, Polynomial& p){
  ...
}

Hope this helps.

edited in order to decrease the liklihood of confusion.
Last edited on
Duthomhas gave you the answer. Note that he moved the declaration for operator >> inside the declaration of your class.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

You have several other errors in your code.
L4: Your class is called Polynomial. L8: Your constructor is Poly. They must match.
L10: Missing a ;
L9,14: (void) is a C-ism that is not needed in C++.
L16: x is not used.
L24-35: You can't define functions within a function.
L7,27,32-34: Your declaration is ord. You use order. They must match.
L27,33: c is undefined. Did you mean coef?
Last edited on
> I need to stablish setOrd and getCoef, so that the operator >> can access it
your operator>> doesn't change the order of the polynomial ¿and why would you need to query the coefficient?
you may implement it by querying the order and setting the coefficients, and for that you already have the .getOrd() and .setCoef() member functions
1
2
3
4
5
6
7
8
9
istream& operator>>(istream& in,Polynomial& p){
  int n = p.getOrd()+1;
  for (int i=0;i<n;i++){
    double coef;
    in >> coef;
    p.setCoef(i, coef); //¿like this or n-(i+1)?
  }
  return in;
}

I see how I misunderstood what you are trying to do, since you already have an accessor and a mutator prototyped.

For input you need mutators:

1
2
3
4
5
6
class Polynomial {
  ...
public:
  void setOrd(int);
  void setCoef(int, float);
};

Thereafter you simply give the methods:

1
2
3
4
5
6
7
void Polynomial::setOrd(int ord){
  ...
}

void Polynomial::setCoef(int p_index, float coef) {
  ...
}

The extraction operator does not need to be a friend in this case; it can directly modify the argument polynomial using the public mutator methods:

1
2
3
4
5
6
7
8
9
10
std::istream& operator>>(std::istream& ins, Polynomial& p) {
  ...
  p.setOrd( /*value obtained from ins*/ );

  /*for each coef obtained from ins*/ {
    p.setCoef(n, coef);
  }

  return ins;
}

Hope this helps better.
Last edited on
You need to be crystal clear about exactly what Polynomial's coef is. Is coef[0] the constant term or the highest order term? I'd make it the constant term so that coef[k] represents the coefficient xk.

Why aren't you using vector<float> for the coefficients? It would simplify the code and handle memory management issues. Right now you can't copy a Polynomial and it leaks memory when destroyed. If you used a vector, you'd fix both for free.

What is the format of a polynomial in a file? Are you allowed to choose it, or is that given to you? It seems odd to me that the disk format doesn't include the order.

Speaking of order, if you can't use a vector then I wouldn't store the order in the class, I'd store the size of the coef array. 99% fo the time that's what code deals with so it's easier to just go with the flow. When you need it, the order is size-1.
@dhayden
It isn't unusual for colleges teaching this stuff to want to do things the hard way.
For an array of floats you need both the pointer to the head and the size, both of which OP has.

You are right that it then requires careful bookkeeping to copy/move/destruct a Polynomial, but this does require the OP/student to learn to do those things with as mundane a task as possible.

Also, continuing with the fact that this is obviously a learning assignment, it is very unlikely that the input file format is something that OP can dink with.

@mikez10
I will second dhayden's recommendation to make your coefficient array have coef[0] as the constant term and coeff[order-1] as the highest rank term.
@mikez10 Please DON'T delete stuff from your post once you've got your answer. It makes the thread incomprehensible for other people, who may want to learn from it.
Please DON'T delete stuff from your post once you've got your answer. It makes the thread incomprehensible for other people, who may want to learn from it.
ah, now I understand why I don't understand.
Looks like someone deemed mikez a bandwidth thief, deleting his posts he had edited to remove the questions.
Good job, mikey boyo, you've gone and treated others like tools because you're a tool.

Is that directed at me? What am I supposed to have done now?
Hahaha, I think that's just an unfortunate coincidence of the OP's username.
Sorry MikeyBoy, I should have been more careful to make it clear that you are awesome, but our fake friend mikez10 who deleted his post is not.

Tá brón orm.
Last edited on
Ah, gotcha! No worries - and thanks :)
MikeyBoy wrote:
What am I supposed to have done now?

Heh.™
Topic archived. No new replies allowed.