传入C++函数的对象值

Object Value Passing in C++ functions

本文关键字:对象 函数 C++ 传入      更新时间:2023-10-16
    #include <string>
    #include <iostream>
    #include <fstream>
    using namespace std;
class cal
{
    int x, y;
public:
    void set(int a, int b)
    {
        x = a;
        y = b;
    }
    cal add(cal c1, cal c2)
    {
        cal temp;
        temp.x = c1.x + c2.x;
        temp.y = c1.y + c2.y;
        return temp;
    }
    void display()
    {
        cout << x << y; //display output
    }
};
int main()
{
    cal c1, c2, c3, c4;
    c1.set(10, 30);
    c2.set(20, 40);
    c4 = c3.add(c1, c2);
    c4.display();
}

我已经在Xcode中尝试过这段代码。 但它不起作用。我也没有收到任何错误。

我越来越"c4.display Thread1:breakpoint 1.1"

谁能告诉我我在这里做错了什么?

我正在做的是将两个对象FirstSecond值一起添加并Display

我也在c4.display()上检查了信息我得到了这个

c4 的打印说明: (校准) c4 = (x = 30, y = 70) (lldb)

如果你在顶部添加#include <iostream>,它将编译并运行良好。