源代码如下:

#include<iostream>
using namespace std;

class score;                     //  如果我注释掉此语句,编译器会报错,但是是乱报错的

class student
{
private:
 int name;
public:
 student(int na)  //构造函数
 {
  name = na;
 }

 void view(const score &A);
};

class score
{
private:
 int stu, sco, teach;
public:
 friend void init(int x, int y, int z, score &B)
 {
  B.stu = x;
  B.sco = y;
  B.teach = z;
 }

 friend void student::view(const score &A);//另一个类的对象作为这个类的成员函数
};

void student::view(const score &A)
{
 if(name == A.stu)
 {
  cout<<"***查询成绩***"<<endl;
  cout<<"学号:"<<name<<endl;
  cout<<"成绩:"<<A.sco<<endl;
  cout<<"打分老师编号:"<<A.teach<<endl;
 }
}

void main()
{
 int x, y, na, te;
 score BB;
 cout<<"教师登陆"<<endl;
 cout<<"输入学生号,成绩,教师编号:"<<endl;
 cin>>x>>y>>te;
 init(x,y,te,BB);
 cout<<" 学生登陆 "<<endl;
 cin>>na;
 student AA(na);
 AA.view(BB);
}

 

我注释掉

//class score;

编译器报错如下:

Compiling...
8-3-4.cpp
E:\vc++自编\8-3-4.cpp(16) : error C2143: syntax error : missing ',' before '&'
E:\vc++自编\8-3-4.cpp(16) : error C2059: syntax error : '&'
E:\vc++自编\8-3-4.cpp(31) : error C2245: nonexistent function 'student::view' specified as friend
E:\vc++自编\8-3-4.cpp(35) : error C2511: 'view' : overloaded member function 'void (const class score &)' not found in 'student'
        E:\vc++自编\8-3-4.cpp(7) : see declaration of 'student'
E:\vc++自编\8-3-4.cpp(56) : error C2664: 'view' : cannot convert parameter 1 from 'class score' to 'const int'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
执行 cl.exe 时出错.

8-3-4.obj - 1 error(s), 0 warning(s)
 

报错结果 信不信由你    哈哈............信了你就完了 ——有的你折腾!!!!