<<运算符重载不起作用;仍在打印对象地址
Overloading << operator isn't working; still printing object address
我试图重载流运算符<<
,但它仍然只是打印对象地址,而不是像我在重载函数定义中那样打印对象信息。这是我的代码,花了相当长的时间尝试我在网上能找到的每个例子,但什么都不起作用。请帮我减轻一些压力!谢谢
间隔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
Interval.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;
}
Interval*
是一个指针。您只是在输出地址。
尝试取消引用指针:
cout << *test << endl;
或者试试这个:
Interval test(10, 1000); // not a pointer.
cout << test << endl;
Interval *test = new Interval(10,1000);
C++中在堆栈上创建对象的语法是
Interval test(10,1000);
除非有充分的理由,否则不要使用指针。
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 呼叫运营商<<临时
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 过载输出<<用于类的运算符,以打印其中的元组
- 重载operator< & lt;作为会员打印
- 重载& lt; & lt;用于打印自定义异常的操作符
- 如何显示/打印字符串对象?cout & lt; & lt;Int工作,count <<字符串
- 什么会导致死亡<<变量& lt; & lt;“ t";当输出超过终端宽度时,打印空白而不是值