English 中文(简体)
载重载体操作员
原标题:Overloading string() operator
  • 时间:2011-10-15 03:39:02
  •  标签:
  • c++

我的教师告诉我们,在档案中,我有这样的任务:

    operator string();

我们绝不能偏离它。

因此,在我的卷宗中,我有:

   Currency::operator string(){
 stringstream output;
 output<<"$";
 output<<dollars;//Why does this return garbage for money1+money2??
 output<<".";
 output<<cents;
 string outputstring = output.str();
 return outputstring;
   }

但是,当我填写<代码>string($1+ money2)时,如果我只是做笔迹(金钱1)”的话,它就行了罚款。 我想知道,我做的是什么错了。

任何帮助都将是巨大的。

这里是我超负荷的+运营商代码:

Currency& Currency::operator+(const Currency &rhs){
Currency temp;
temp.dollars = dollars + rhs.dollars;
temp.cents = cents + rhs.cents;
temp.simplify();
return temp;
}

<>说明: 我已经开了小差,没有与超负荷的+运营商有任何问题;它回报了它应该做些什么。

最佳回答

这里是我超负荷的+运营商代码:

你们正在逐个回归一个地方变量。 <>0>

你们应当按价值返回:

Currency Currency::operator+(const Currency &rhs)
问题回答

暂无回答




相关问题
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?

热门标签