[英]Return Object of Class to Main Program
I am not so familiar with C++ and currently trying to code a class 'fraction'. 我对C ++不太熟悉,目前正在尝试编写类“分数”。 I want to code a method 'Input' where a user gives values to a fraction.
我想编写一个方法“输入”,其中用户为小数赋值。 These values should be available throughout the program.
这些值应在整个程序中可用。 This is what I have so far:
这是我到目前为止的内容:
Call: 呼叫:
BRUCH t1;
t1.Eingabe();
Method: 方法:
BRUCH BRUCH::Eingabe()
{
int _z, _n;
BRUCH bruch;
cout << "Bitte einen Zaehlerwert eingeben: " << endl;
cin >> _z;
cout << "Bitte einen Nennerwert eingeben: " << endl;
cin >> _n;
while(_n==0)
{
cout << "Bitte einen gültigen Wert eingeben!" << endl;
cin >> _n;
}
bruch.z = _z;
bruch.n = _n;
return bruch;
}
When I return the object the values vanish and the constructor creates a new object with default values. 当我返回对象时,值消失了,构造函数创建了一个具有默认值的新对象。
What must I do to get the correct return? 我该怎么做才能获得正确的回报?
Bruch - Fraction (math.) | Bruch-分数(数学)| Eingabe - Input
Eingabe-输入
It doesn't look like you ever assigned the new value to anything 看起来您从未将新值分配给任何东西
BRUCH t1;
BRUCH new_value = t1.Eingabe();
I solved the problem. 我解决了问题。
I changed the return bruch;
我
return bruch;
to return *this;
return *this;
and removed the 'bruch' obj. 并删除了“ bruch” obj。 from the code.
从代码。 The assignment is now
z = _z, n = _n;
现在的分配是
z = _z, n = _n;
. 。 This returns the current object I am working with.
这将返回我正在使用的当前对象。
Sorry for any inconvenience. 任何不便敬请谅解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.