注册 登录
编程论坛 C++教室

运算符重载实例>>出现问题,求助帖!

hduhjj 发布于 2010-05-03 19:51, 623 次点击
#include<iostream>
using namespace std;

class c
{
public:   
    friend ostream& operator<<(ostream& o,const c& exam);
    void SetTime();
    void ShowTime();
private:
    int h,m,s;
};

void c::SetTime()
{
    h=1;
    m=2;
    s=3;
}

ostream&
operator<<(ostream& o,const c& exam)
{
    o<<exam.h<<':'<<exam.m<<':'<<exam.s;
    return o;
}

void main()
{
    c exam;
    exam.SetTime();
    cout<<exam<<endl;
}
//运行这个程序,出现以下内容并且一闪而消失,困惑中……:
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
The thread 0x838 has exited with code 4701840 (0x47BE90).
The program 'D:\c\Debug\运算符重载实例.exe' has exited with code 4701840 (0x47BE90).
4 回复
#2
hduhjj2010-05-03 19:59
{
    o<<exam.h<<':'<<exam.m<<':'<<exam.s;
    return o;
}
当我在代码中的这部分,做“以下改动”的时候,问题消失了,这是怎么回事?呵呵……有人解答么?
{   
    return o<<exam.h<<':'<<exam.m<<':'<<exam.s;
}
#3
2010-05-04 22:27
一闪而过,你可以最后加个get(),或者按ctrl+f5执行。
你的程序在2005中执行是正确的。
#4
2010-05-05 11:25
system("pause");
#5
卫生巾2012-11-17 17:52
你这能运行过去???对象可以直接引用类的私有数据成员???
1