超载<<插入运算符无法正常工作

发布于 2024-11-16 06:07:42 字数 2455 浏览 1 评论 0 原文

所以我不明白为什么我的插入运算符不适用于我的列表类。我已经研究了一段时间,我认为重载的语法是正确的。对此不太确定。关于为什么它不起作用的任何提示?这是代码:

编辑:将一些代码更改为当前的代码。

抱歉,现在的问题是我无法让它打印任何内容,它只是打印和空行。

这是驱动程序:

#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;
}

So I can't figure out why my insertion operator isn't working for my list class. I've looked at it for a while and I think the syntax is correct for overloading. Not sure on this one. Any hints as to why it's not working?? Here's the code:

EDIT:Changed some code around to what it currently is right now.

Sorry, the problem specifically now is that I cannot get it to print anything out, it simple prints and empty line.

here's the driver:

#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;   
}

Here's the header:

#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

Here's the implementation:

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2024-11-23 06:07:42

poly 是一个指针,要使用自定义运算符<<,您需要说“

cout << *poly; // output the object pointed-to, not the pointer itself

您还没有重载”插入多项式*意味着什么代码>.你也不应该尝试。

除此之外,您可能应该通过 const 引用接受对象,流输出运算符没有理由去更改对象。

poly is a pointer, to use your custom operator << you need to say

cout << *poly; // output the object pointed-to, not the pointer itself

You haven't overloaded what it means to insert a Polynomial*. Nor should you try.

Besides that, you probably should accept the object by const reference, there's no reason for a stream output operator to go changing the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文