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 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统