qq_45813272 2021-01-18 21:10 采纳率: 40%
浏览 27
已采纳

c++ 程序改错 求大佬解答

#include <iostream>
class A
{int a,b; 
public:
A(int x,int y);
void display();
friend void show(A t);
};
A::A(int x,int y)
{a=x;b=y;}
void display()
{cout<<a<<endl;
}
void show(A t)
{
cout<<t.b<<endl;
}
void main()
{
A temp(1,2);
temp.display();
temp.show(temp);
cout<<temp.a<<endl; 
}

  • 写回答

1条回答 默认 最新

  • 云中小筑 2021-01-18 22:24
    关注

    下面是修改过的代码,可参考理解

    #include <iostream>
    using namespace std; //后面的cout需要用到 
    class A
    {
    	int a,b;   //默认是private属性
    public:
    	A(int x,int y);
    	void display(); 
    	friend void show(A t);
    };
    A::A(int x,int y)
    {
    	a=x;
    	b=y;
    }
    void A::display()
    {
    	cout<<a<<endl;
    }
    void show(A t)
    {
    	cout<<t.b<<endl;
    }
    int main()
    {
        A temp(1,2);
        temp.display();
        show(temp); //友元函数不属于类的成员,不能用 . 来调用 
        //cout<<temp.a<<endl; //C++默认是private属性的,不能直接调用 
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示