简体   繁体   English

当我没有编码时,为什么我的班级会执行算术运算?

[英]Why is my class performing arithmetic when I haven't coded any?

I'm working on a homework project that is supposed to introduce us to classes. 我正在做一个家庭作业项目,该项目应该向我们介绍课程。 I've just started, but for some reason my member variables are displaying correctly until the end. 我才刚刚开始,但是由于某种原因,我的成员变量正确显示直到结束。 It should just be passing those variables between functions then outputting them. 它应该只是在函数之间传递这些变量,然后输出它们。 It will take a numerator and denominator set by a parameterized constructor, set an object with them, then output what the numerator and denominator holds. 它需要一个由参数化构造函数设置的分子和分母,并用它们设置一个对象,然后输出分子和分母所保存的内容。 It is taking the numerator and setting it to zero, even when it previously displayed the correct number in every function the class ran through. 它使用分子并将其设置为零,即使该分子先前在类运行的每个函数中显示了正确的数字也是如此。 The denominator is even more weird because it turns into some huge number somehow. 分母更加奇怪,因为它以某种方式变成了很多数字。 I just want to understand what is happening since I am new to this and the reason why isn't super apparent to me. 我只是想了解由于我是新手而发生的事情,以及为什么对我而言并不那么明显的原因。

#include <iostream>

using std::cout;
using std::endl;
using std::ostream;

class RationalNum
{
public:
    RationalNum();
    RationalNum(int numer, int denom);
    int getNumerator() const { return numer; }
    int getDenominator() const { return denom; }
    void setNumerator(int);
    void setDenominator(int);
    void set(int& numer, int& denom);
    void output(ostream& outs);

private:
    int denom;
    int numer;
};

RationalNum::RationalNum() : numer(1), denom(1)
{}

RationalNum::RationalNum(int numer, int denom) //takes two integers for numer and denom, and reduces
{
    cout << "The numer and denom  in the param const are " << numer << denom << endl;
    set(numer, denom);
}

void RationalNum::set(int& numer, int& denom) //use other functions to take two params and set as numer and denom

{
    cout << "The numer and denom in set are " << numer << denom << endl;
    setNumerator(numer);
    setDenominator(denom);
}

void RationalNum::setNumerator(int numer) //takes an int, sets it as numer, and reduces it
{
    cout << "in setNumer, the numer is: " << numer << endl;
    //reduce(newNumer); //Not using yet
}

void RationalNum::setDenominator(int denom) //takes an int, sets it as denom, and reduces it
{
    cout << "in setDenom, the denom is: " << denom << endl;
    //reduce(newDenom); //Not using yet
}

void RationalNum::output(ostream& outs) //takes an ostream param
{
    cout << numer << "/" << denom << endl;
}


int main()
{
    RationalNum rn1, rn2(25, 10), rn3(24, -100);

    cout << "rn1 contains: ";
    rn1.output(cout);
    cout << endl;

    cout << "rn2 contains: ";
    rn2.output(cout);
    cout << endl;

    cout << "rn3 contains: ";
    rn3.output(cout);
}

This is what the actual results are. 这就是实际结果。 It is correct until the last two lines, and that is where I'm lost. 直到最后两行都是正确的,那是我迷路的地方。 rn2 should display 25/10 and rn3 should display 24/-100 rn2应该显示25/10,而rn3应该显示24 / -100

The numer and denom  in the param const are 2510
The numer and denom in set are 2510
in setNumer, the numer is: 25
in setDenom, the denom is: 10

The numer and denom  in the param const are 24-100
The numer and denom in set are 24-100
in setNumer, the numer is: 24
in setDenom, the denom is: -100
rn1 contains: 1/1    
rn2 contains: 0/4196160    
rn3 contains: 0/4197376

The methods setNumerator and setDenominator are not doing anything. setNumeratorsetDenominator方法没有做任何事情。 For this reason, the class member denom and numer and never initialized. 出于这个原因,类成员denomnumer永不初始化。 Their value will thus be undefined, ie, potentially garbage. 因此,它们的值将是不确定的,即可能是垃圾。

To fix it, use: 要解决此问题,请使用:

void RationalNum::setNumerator(int n)
{
  numer = n; // I changed the parameter name to avoid confusion with the member
  cout << "in setNumer, the numer is: " << numer << endl;
  //reduce(newNumer); //Not using yet
}

Use a similar approach for the denominator. 对分母使用类似的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我没有释放任何 memory 时,为什么 Memory 博士报告已释放 memory 错误? - Why is Dr Memory reporting freed memory errors when I haven't freed any memory? 从算术运算符返回时,为什么不能将此类作为引用传递? - Why can't I pass this class as a reference when returned from arithmetic operators? 即使我没有显示它的路径,windbg也会找到我的应用程序pdb文件 - windbg finds my application pdb file even when I haven't revealed its path 当我没有动态声明我的数组时,这个 display() 如何显示数组值 - How this display() displays the array values when I haven't declared my array dynamically 为什么编译器认为我没有为vector var指定类型? - Why compiler thinks I haven't named the type for a vector var? 为什么 cpp 允许访问我尚未分配的内存? - why cpp allows to get a access to memory i haven't allocated? 为什么std :: make_unique <std::thread> (somefun())在我没有调用release()时会崩溃,而在调用时会崩溃吗? - Why std::make_unique<std::thread>(somefun()) will generate crash when I haven't call release(), and well when called? 当我将角色编码设置为0度时,为什么我的角色以45度角生成? - Why is my character spawning at a 45 degree angle when I've coded it to be set to 0 degrees? 为我自己的函数执行通常的算术转换? - Performing the usual arithmetic conversions for my own function? 有一个简单的问题。 不知道怎么解决,网上也没看到解决办法 - Having a simple issues. Don't know how to solve and I haven't seen any solutions online
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM