【问题标题】:Output provides size of integer instead of the actual integer输出提供整数的大小而不是实际的整数
【发布时间】:2021-06-05 07:59:18
【问题描述】:

// 创建名为 Class1 和 Class2 的类,每个类都有一个私有成员。添加成员函数以在每个类上设置一个值(比如 setValue)。再添加一个对两个类都友好的函数 max(),max() 函数应该比较两个类的两个私有成员并显示它们之间的最大值。为每个类创建一个对象并为其设置一个值。显示其中的最大个数

#include <iostream>
using namespace std;

class Class1
{
    int a;

    public:
        int setValue();
    
    friend int max();
};

class Class2
{
    int b;

    public:
        int setValue();

    friend int max();
};

int Class1::setValue()
{
    cout<<"Enter the first number:";
    cin>>a;
}


int Class2::setValue()
{
    cout<<"Enter the second number:";
    cin>>b;
}

int max()
{
    Class1 c1;
    Class2 c2;

    if(c1.a>c2.b)
    {
        cout<<"Greater number: "<<c1.a<<endl;
    }

    else
    {
        cout<<"Greater number: "<<c2.b<<endl;
    }
}

int main()
{
    Class1 obj1;
    Class2 obj2;

    obj1.setValue();
    obj2.setValue();
    cout<<endl;
    max();

    return 0;
}

【问题讨论】:

  • max 函数创建两个新对象并比较它们。那是你要的吗?也许您想将要比较的对象作为参数传递给函数。

标签: c++


【解决方案1】:

您应该尝试传递要比较的Class1Class2 对象,而不是在max 函数内创建新的本地Class1Class2 对象。

试试这样的方法:

#include <iostream>
using namespace std;

class Class1;
class Class2;

class Class1
{
    int a;

    public:
        void setValue();
    
    friend int max(Class1 c1, Class2 c2);
};

class Class2
{
    int b;

    public:
        void setValue();

    friend int max(Class1 c1, Class2 c2);
};

void Class1::setValue()
{
    cout<<"Enter the first number:";
    cin>>a;
}


void Class2::setValue()
{
    cout<<"Enter the second number:";
    cin>>b;
}

int max(Class1 c1, Class2 c2)
{
    int maxValue;
    
    if(c1.a>c2.b)
    {
        cout<<"Greater number: "<<c1.a<<endl;
        maxValue = c1.a;
    }

    else
    {
        cout<<"Greater number: "<<c2.b<<endl;
        maxValue = c2.b;
    }

    return maxValue;
}

int main()
{
    Class1 obj1;
    Class2 obj2;

    obj1.setValue();
    obj2.setValue();
    cout<<endl;
    int maxValue = max(obj1, obj2);

    return 0;
}

【讨论】:

  • 只是好奇为什么这个答案被否决了?感谢您的反馈,这样我就不会再重复同样的错误了。
  • 我猜是因为int max 是一个声称返回int 但没有返回路径的函数。在 op 的代码中,他们的 setValue 函数共享这个问题 - 可能值得指出,所以答案是完整的(尽管第二点不值得对 imo 投反对票)。
  • 感谢乔治的反馈,这是有道理的。我可以看到指出这些问题可以如何增加答案。由于另一个答案已经指出了这一点,我不会尝试重复同样的事情。我修改了答案中的 sn-p 以使其更完整,并希望更好地解决 OP 发布的原始问题。
  • 我还看到我建议更改 OP 的 sn-p 的其他一些东西,例如为什么有一个函数 max,它不仅计算最大值,还执行操作打印最大值。那里可以更好地分离关注点。我也不确定将两个不同的类与友元函数一起使用的意义何在,而不是仅仅使用带有重载比较运算符的单个类或采用同一类的 2 个实例的最大静态方法。不确定这是否也适合指出?
  • 这真的取决于判断力。在这种情况下,我会争辩说,值得指出操作员可以用他们的代码做不同的事情,因为他们是初学者。但是很多时候深入研究问题代码中的所有错误是不切实际的。在回答指南方面 - 只回答操作员提出的问题就足够了。在答案中仅包含法律代码也会获得一些分数。无论如何,答案对我来说或多或少都不错,所以我投了赞成票。 (尽管我仍然希望看到设置器更改为void - 即使答案中没有提到:))
【解决方案2】:

我发现您的代码存在一些差异。首先,你想在setValue() 方法中返回什么。您已将返回类型 int 而不是 void 设置为那些肯定会给出错误的 setValue() 方法。找到最大值的友元函数还需要返回一个int,正如您在max() 的原型中声明的那样。我相信你不应该从setValue() 方法返回任何东西。


另一个你做错了的事情是:

Class1 c1;
Class2 c2;

max 方法内部,而不是将对象作为参数传递给max 方法。 您尚未将对象传递给 max() 方法的参数。在max 方法内创建新的class1 和class2 对象将无济于事。同时,max 方法的输出将是未定义的。


解决方案:

#include <iostream>
using namespace std;

class Class1
{
    int a;

    public:
        void setValue();
    
    friend int max(Class1 obj1, Class2 obj2);
};

class Class2
{
    int b;

    public:
        void setValue();

    friend int max(Class1 obj1, Class2 obj2);
};

void Class1::setValue()
{
    cout<<"Enter the first number:";
    cin>>a; // this sets the value of private data-member a.
}


void Class2::setValue()
{
    cout<<"Enter the second number:";
    cin>>b; // this sets the value of private data-member b.
}

int max(Class1 obj1, Class2 obj2)
{

    int max;


    if(obj1.a>=obj2.b)
    {
        cout<<"Greater number: "<<obj1.a<<endl;
        max=obj1.a;
    }

    else
    {
        cout<<"Greater number: "<<obj2.b<<endl;
        max=obj2.b;
    }
    return max;
}

int main()
{
    Class1 obj1;
    Class2 obj2;

    obj1.setValue();
    obj2.setValue();
    cout<<endl;
    int maximum=max(obj1, obj2);
    cout<<"Maximum is: "<< maximum<< endl;
    return 0;
}

希望,这会有所帮助。 :-)

【讨论】:

  • 我试图学习使用对象作为参数!谢谢你
  • @SibindraTimalsina,您可以将对象作为参数传递,就像我传递给 max 方法的方式一样。欲了解更多信息:请通过geeksforgeeks.org/passing-and-returning-objects-in-c
  • @SibindraTimalsina 接受这个答案,如果它解决了你的问题。因此,它将帮助其他人找到相同类型问题的正确答案。
【解决方案3】:

您已声明 max 以及两个成员函数 setValue 返回 int。这些函数执行到最后而不返回值。程序的行为未定义。

解决方案:要么返回一个值,要么声明函数返回void


这里:

Class1 c1;
Class2 c2;

您默认初始化两个变量。这些对象的成员将被默认初始化,在 int 的情况下意味着它们具有不确定的值。

在这一行:

if(c1.a>c2.b)

您将不确定的值与另一个不确定的值进行比较。输出更大的值时也是如此。程序的行为未定义。

解决方案:初始化您使用其值的所有对象。在这种情况下,我怀疑您不应该创建新的、未初始化的变量,而是要读取在main 中创建的现有对象。通常,这是通过将值作为参数传递给函数来实现的。

【讨论】:

  • 当我使用 void 而不是 int 时它起作用了。
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多