#include<iostream.h>
class one
{
public:
one()
{
x=1;
}
    friend out(one b);
private:
int x;
}
out(one b)
{
    cout<<b.x<<endl;
}
main()
{
 one a;
 out(a);
}错在什么地方?友元函数怎么用?

解决方案 »

  1.   

    错得太多, 为什么一定要用友元?你又不是定义<< 和 >>操作符.#include<iostream>
    class one
    {
    public:
        one()
       {
          x=1;
        }
        void out();
    private:
        nt x;
    };void one::out()
    {
        std::cout << x << std::endl;
    }int main()
    {
       one a;
       a.out();
       system("pause");
    }
      

  2.   

    out(one b)
    {
        cout<<b.x<<endl;
    }
    既然是一个函数,有返回值就声明返回值类型,没有返回值就定义为void型
      

  3.   

    #include<iostream.h>
    class one
    {
    public:
    one()
    {
    x=1;
    }
        friend out(one b);
    private:
    int x;
     };
    out(one b)                          
    {
    b.x +=1; 
        cout<<b.x<<endl;
    return b.x;
     }
    void main(void)
    {
    one a;
    out(a);
    }
      

  4.   

    改成这样:#include<iostream.h>
    class one
    {
    public:
    one()
    {
    x=1;
    }
        friend void out(one b);
    private:
    int x;
    }
    void out(one b)
    {
        cout<<b.x<<endl;
    }
    main()
    {
     one a;
     out(a);
    }