简体   繁体   English

链表上的Ostream重载运算符

[英]Ostream overloading operator on a linked list

I have a polynomial implementation in a linked list and want to do std::ostream overloading operation but it gives my an error that no match for 'operator<<' in 'std::cout << p5' 我在链表中​​有一个多项式实现,想执行std::ostream重载操作,但是它给我一个错误,即no match for 'operator<<' in 'std::cout << p5'

That's my implementation but when I test it through cout << p5 I get the aforementioned error. 那是我的实现,但是当我通过cout << p5对其进行测试时,出现了上述错误。

UPDATE: header file: 更新:头文件:

struct term{
    double coef;
    unsigned deg;
    struct term * next;
};
class Polynomial {
public:
    constructors etc
    overloading functions
   friend ostream& operator << (ostream& out,const term& object);
}

then in other file poly.cpp i have: 然后在其他文件poly.cpp我有:

 ostream & operator << (ostream& out, const Polynomial object){
        term* q = object.getptr();
        if (object.getptr() == NULL)
            out << "( )";
        else
            while(q != NULL)
            {
                out << q->coef << "x^" << q->deg << " ";
                q = q->next;
            }
        return out;
    }

in main.cpp Polynomial p5, then added some terms and cout << p5 but I get errors. 在main.cpp多项式p5中,然后添加了一些术语和cout << p5但出现错误。

I think it's your declarations which are causing the problem: 我认为是您的声明引起了问题:

friend ostream & operator << (ostream & out, const term & object);

and

ostream & operator << (ostream & out, const Polynomial & object);

These don't match. 这些不匹配。 One is using a term object and the latter is using a Polynomial object. 一种是使用term对象,而另一种是使用Polynomial对象。 I'm assuming you want this function to use a term object because the function uses data memebers specific to the struct term . 我假设您希望此函数使用term对象,因为该函数使用特定于struct term So change the latter to accept a term object: 因此,更改后者以接受term对象:

ostream & operator << (ostream & out, const term & object);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM