Overload Operator >> in C ++

#include<iostream> using namespace std; class term { public: int exp; int coeff; }; class poly { public: term* term_ptr; int no_term; poly(int d); friend istream& operator>>(istream& in, poly& p); friend ostream& operator<<(ostream& out, const poly& p); friend poly operator+(const poly& p1, const poly& p2); }; poly::poly(int d=0) { no_term = d; term_ptr = new term[no_term]; } istream& operator>>(istream& in, poly& p) { in>>p.no_term; for(int i= 0; i<p.no_term; i++) { in>>(p.term_ptr+i)->coeff; in>>(p.term_ptr+i)->exp; } return in; } 

i overloaded the input statement to enter the object. The problem I am facing is when I input two objects, sequentially changing the input data element of the first object.

 int main(void) { poly p1, p2; cin>>p1; cin>>p2; cout<<p1; cout<<p2; return 0; } 

if input

  3 1 1 1 2 1 3 3 1 1 1 2 1 3 

conclusion i get

 1 1 1 2 1 1 1 1 1 2 1 3 

output operator function

 ostream& operator<<(ostream& out, const poly& p) { out<<"coeff"<<" "<<"power"<<endl; for(int i = 0; i< p.no_term; i++) out<<(p.term_ptr+i)->coeff<<" "<<(p.term_ptr+i)->exp<<endl; return out; } 
+4
source share
3 answers

Initially, you allocate an array with null elements. When reading objects, you read the number of terms, but do not redistribute the array of terms. I personally would recommend using a suitable container type, for example std::vector<term*> or, in fact, std::vector<std::shared_ptr<term>> . If you stick with arrays, you need something like this:

 std::istream& operator>>(std::istream& in, poly& p) { if (in>>p.no_terms ) { std::unique_ptr<term[]> terms(new term[p.no_terms]); for(int i= 0; i<p.no_term; i++) { in >> terms[i].coeff; in >> terms[i].exp; } if (in) { delete[] p.term_ptr; p.term_ptr = terms.release(); } } return in; } 
+2
source

Change poly p1, p2; on poly p1(3), p2(3); .

p.no_term has a value of 3 , however look at your poly constructor:

 poly::poly(int d=0) { no_term = d; term_ptr = new term[no_term]; } 

You create an array of length 0 . In addition, there is no need to use a pointer in your code. Here is an example using std::vector<term> :

 #include<iostream> #include <vector> using namespace std; class term { public: int exp; int coeff; }; class poly { public: std::vector<term> term_vec; int no_term; poly(int d); friend istream& operator>>(istream& in, poly& p); friend ostream& operator<<(ostream& out, const poly& p); friend poly operator+(const poly& p1, const poly& p2); }; poly::poly(int d=0) : term_vec(d), no_term(d) { } istream& operator>>(istream& in, poly& p) { in>>p.no_term; p.term_vec.resize(p.no_term); for(int i= 0; i<p.no_term; i++) { in>> p.term_vec[i].coeff; in>> p.term_vec[i].exp; } return in; } ostream& operator<<(ostream& out, const poly& p) { out<<"coeff"<<" "<<"power"<<endl; for(int i = 0; i< p.no_term; i++) out<<p.term_vec[i].coeff<<" "<<p.term_vec[i].exp<<endl; return out; } int main(void) { poly p1, p2; cin>>p1; cin>>p2; cout<<p1; cout<<p2; return 0; } 
+1
source

The default constructor property d is 0 . Then you call new term[0] . Pointers that are initialized as arrays with a length of 0 are indicated in one place in your example. After filling in the invalid memory and viewing the same results.

0
source

Source: https://habr.com/ru/post/1440702/


All Articles