【问题标题】:Overloading << operator isn't working; still printing object address重载 << 运算符不起作用;仍在打印对象地址
【发布时间】:2013-02-10 01:03:54
【问题描述】:

我正在尝试重载流运算符&lt;&lt;,但它仍然只是打印对象地址,而不是像我在重载函数定义中那样的对象信息。这是我的代码,我花了很长时间尝试我在网上可以找到的每个示例,但没有任何效果。请帮助减轻我的一些压力!谢谢。

间隔.h:

#ifndef INTERVAL_H
#define INTERVAL_H
#include <string>
#include <iostream>
#include <assert.h>

using namespace std;

class Interval {
    public:
        long start;
        long end;

        // Constructor
        Interval(long, long);

        // Deconstructor
        // Virtual to make sure that it calls the correct destructor
        virtual ~Interval();

        bool operator==(const Interval &other) const;
        bool operator!=(const Interval &other) const;
        bool operator<(const Interval &other) const;
        bool operator<=(const Interval &other) const;
        bool operator>(const Interval &other) const;
        bool operator>=(const Interval &other) const;
        friend ostream& operator<<(ostream& os, const Interval &i);

};
#endif

间隔.cpp:

#include "Interval.h"
#include <iostream>
#include <string>

using namespace std;

Interval::Interval(long a, long b){
    // Assert that start is less than the end
    assert(a < b);
    start = a;
    end = b;
}

// Deconstructor
Interval::~Interval(){
}

bool Interval::operator==(const Interval &other) const{
    // Return true if both start and end are equal to other's
    if(start == other.start && end == other.end){
        return true;
    }else{
        return false;
    }
}

bool Interval::operator!=(const Interval &other) const{
    // Return true(not equal) if either the start or end are different
    if(start != other.start || end != other.end){
        return true;
    }else{
        return false;
    }
}
bool Interval::operator<=(const Interval &other) const{
    // Return true if the start is less than or equal other's start
    if(start <= other.start){
        return true;
    }
}

bool Interval::operator>(const Interval &other) const{
    // Return true if the end is greater than other's end
    if(end > other.end){
        return true;
    }
}

bool Interval::operator>=(const Interval &other) const{
    // Return true if the end is greater than or equal to other's end
    if(end >= other.end){
        return true;
    }
}


bool Interval::operator<(const Interval &other) const{
    // Return true if the start is less than other's
    if(start < other.start){
        return true;
    }
}

ostream& operator<<(ostream& os, const Interval &i){

    os << "Interval[" << i.start << ", " << i.end << "]" << endl;
    return os;
}

int main(void){
    Interval *test = new Interval(10,1000);
    cout << test << endl;
    cout << "test" << endl;
}

【问题讨论】:

    标签: c++ operator-overloading iostream


    【解决方案1】:

    Interval* 是一个指针。您只是在输出地址。

    尝试取消引用指针:

    cout << *test << endl;
    

    或者试试这个:

    Interval test(10, 1000);   // not a pointer.
    cout << test << endl;
    

    【讨论】:

    • 我真的不敢相信我忽略了这一点。非常感谢!
    【解决方案2】:
    Interval *test = new Interval(10,1000);
    

    C++中在栈上创建对象的语法是

    Interval test(10,1000);
    

    除非有充分的理由,否则不要使用指针。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多