English 中文(简体)
D. 第C.++号[复制]
原标题:overloading the extraction operator >> in C++ [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Operator overloading

我必须制定锁定方案,在卸下开采经营者时,我可以进入小时、分钟和秒钟。 这些是我的法典:

clockType.h

#include<iostream>
using namespace std;

class clockType
{
public:
   clockType();
   void getTime();
   friend istream& operator>>(istream&, const clockType);
private:
   int hr, min, sec;
}

clockType.cpp

#include<iostream>
#include clockType.h"
using namespace std;

clockType::clockType()
{
    hr = 0;
    min = 0;
    sec = 0;
}

void clockType::getTime()
{
    while(hr>=24)
        hr = hr - 24;
    while(min>=60)
        min = min - 60;
    while(sec>=60)
        sec = sec - 60;
    cout<<setfill( 0 )
        <<setw(2)<<hr<<":"
        <<setw(2)<<min<<":"
        <<setw(2)<<sec<<endl;
 }

 istream& operator>>(istream& in, clockType cl)
 {
    in>>cl.hr>>cl.min>>cl.sec;
    return in;
 }

入境点.cpp

 #include<iostream>
 #include clockType.h 

 using namespace std;

 int main()
 {
   clockType clock;
   cout<<"Enter hr, min, sec";
   cin>>clock;
   clock.getTime();
   return 0;
 }

不存在错误。 我的问题是,当我进入时,它为什么产出00:00:00? 为什么 t;和;将其价值传给目标锁?

最佳回答

需签署<代码>operator>>

istream& operator>>(istream& in, clockType& cl) 

也就是说,它应当接受参引<>>>至<条码>>。

页: 1 举例来说,当经营者被援引<代码>clock的临时复制件时,经营者就这样做。 副本随后被弃置,原文<编码>clock仍未修改。

这里的另一个问题是,如果你成功地阅读了你的投入,你就无法检查。 <代码>>>上的任何和所有操作 可能失败的是,在不知的国家可以留有<代码>cl。 因此,首先,你们都应用<代码>(in>>cl.hr)等东西来测试成功。

这仍然不够,因为第一次读到操作(载于: <条码>hr>/条码>)。 可能取得成功,但次可能失败;这将在不知的状态下留下你的“条码”栏。 如果开采业者有交易杂质,即更新网站www.em>所有三个“>/em”成员,或者将物体留在原国家,那将是好事。 这样做的一种方式是首先阅读当地变数,而且只有所有三者都读过后复制到“条码”中的价值。

问题回答

你的开采业者或许应当改变你交给它的标语,而不是仅仅修改标书。 这就是说,你想把你的开采经营人的第二个论点作为非最轻的参考,也是:

std::istream& operator>> (std::istream& in, clockType& cl) { ... }

BTW,always, 核对提取工作之前使用结果:

if (std::cin >> clock) { ... }
istream& operator>>(istream& in, clockType cl)

应:

istream& operator>>(istream& in, clockType &cl)   // (note the &)

您必须参考<代码>operator>>,否则将只读到当地功能物体。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签