【问题标题】:overloaded << insertion operator isn't working correctly重载 << 插入运算符无法正常工作
【发布时间】:2011-06-18 21:25:14
【问题描述】:

所以我无法弄清楚为什么我的插入运算符不适用于我的列表类。我已经看了一段时间,我认为语法对于重载是正确的。不确定这个。关于为什么它不起作用的任何提示?代码如下:

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

对不起,现在的具体问题是我无法让它打印出任何东西,它只是简单的打印和空行。

这是司机:

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

【问题讨论】:

  • “它不工作”不是有效的问题描述。编译失败了吗?它会在运行时崩溃吗?它是否在没有崩溃的情况下运行,但会产生意想不到的结果?尝试在发布问题时添加此类信息。我也推荐阅读Writing the perfect question
  • @Martinho Fernandes 这是我在 main 方法中使用插入的时候。它应该打印出列表中的 2 个术语的数据,但它会打印出内存地址。
  • @Keoki Zee。我给你放一个:)
  • @Ben Voigt 谢谢。我接受了你的建议,现在它只打印出一个空白链接,这让我想知道当我使用 set_coefficient 时我是否真的存储了任何东西。
  • @infinite_loop: 我会让operator&lt;&lt; 无条件地打印一些东西(在for循环之外),然后抓住我的调试器,在set_coefficient 和operator&lt;&lt; 中设置断点,看看发生了什么开。

标签: c++ linked-list ostream


【解决方案1】:

poly 是一个指针,要使用你的自定义operator &lt;&lt; 你需要说

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

您还没有超载插入Polynomial* 的含义。你也不应该尝试。

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

【讨论】:

    猜你喜欢
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2015-04-25
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    相关资源
    最近更新 更多