<<运算符仅打印空行



在main中,列表使用插入运算符打印出来,但我得到的只是一行空行。不知道为什么。是不是即使使用set_coefficient,列表中也没有存储任何内容?欢迎任何其他批评。感谢

这是司机:

#include <iostream>
#include "polynomial.h"
using namespace std;
int main(){
Polynomial* poly = new Polynomial();    
poly->set_coefficient(3,2);
poly->set_coefficient(0,2);
poly->set_coefficient(3,1);
cout << "trying to print data" << endl;
cout << *poly << endl;    
return 0;   
}

这是标题:

#ifndef _POLYNOMIAL_H_
#define _POLYNOMIAL_H_
#include <iostream>
class Polynomial {
public:
struct PolyNode {
    int coefficient, degree;
    struct PolyNode* next;      
    PolyNode(int c, int d, PolyNode* n): coefficient(c),degree(d),next(n){}
};
PolyNode* firstTerm;
Polynomial(): firstTerm(0) {} 
struct PolyNode* get_first(){
    return firstTerm;
}

//makes the term with degree d have a coefficient of c
void set_coefficient(int c, int d);     
~Polynomial();  
friend std::ostream& operator<<(std::ostream& o, const Polynomial& p);          
};
#endif

以下是实现:

#include "polynomial.h"
#include <iostream>
#include <ostream>
using namespace std;
void Polynomial::set_coefficient(int c, int d){
PolyNode* start = firstTerm;
if(c != 0 && firstTerm == 0)    
    firstTerm = new PolyNode(c,d,NULL);
else{   
    cout << "Entered set_coefficient()" << endl;
    while(start->degree != d && start->next != NULL){
        cout << "Inside set_coefficient() while loop" << endl;          
        start = start->next;        
    }       
    if(c != 0 && start == 0)    
        start = new PolyNode(c,d,0);
    else if(c!= 0 && start != 0)
        start->coefficient = c;
    else if(c == 0){
        cout << "deleting a term" << endl;          
        delete start;
    }
}   
    cout << "Leaving set_coefficient()" << endl;
}
ostream& operator<<(ostream& o,const Polynomial& p){
Polynomial::PolyNode* start = p.firstTerm;  
for(unsigned int i = 0; start->next != 0; i++){
    o << "Term " << i << "'s coefficient is: " << start->coefficient << " degree is: " << start->degree << endl << flush;
    start = start->next;
}   
return o;
}
     if(c != 0 && start == 0)
        start = new PolyNode(c,d,0);
    else if(c!= 0 && start != 0)
        start->coefficient = c;
    else if(c == 0){
        cout << "deleting a term" << endl;
        delete start;
    }
if (c != 0 && start == 0) // this situation may occur ? you have already judged surrounding if
else if (c == 0) {
    cout << "deleting a term" << endl;
    delete start;      // that may be cause linkedList detach.
}
cout << "Entered set_coefficient()" << endl;
while(start->degree != d && start->next != NULL){
    cout << "Inside set_coefficient() while loop" << endl;          
    start = start->next;        
}       
if(c != 0 && start == 0)    
    start = new PolyNode(c,d,0);
else if(c!= 0 && start != 0)
    start->coefficient = c;
else if(c == 0){
    cout << "deleting a term" << endl;          
    delete start;
}

看起来你想要一个链表,但你既没有创建也没有删除链接(start->next),只是分配内存。如果不设置链接,就没有列表。

这里可能有多个错误:>

ostream& operator<<(ostream& o,const Polynomial& p){
Polynomial::PolyNode* start = p.firstTerm;  
for(unsigned int i = 0; start->next != 0; i++){

这不会打印最后一个PolyNode。。。

相关内容

  • 没有找到相关文章

最新更新