C++ 一个类的成员函数作为另一个类的友元函数,为什么错了?
//问题见注释#include<iostream>usingnamespacestd;classTeacher;//这里编译不成功,提示incompletetype'Te...
//问题见注释
#include <iostream>
using namespace std;
class Teacher; //这里编译不成功,提示incomplete type 'Teacher' named in nested name specifier
class Student
{
friend void printStudent(Student grade);
friend void Teacher::printStudentInTeacher(Student &s);
public:
private:
int grade;
};
class Teacher
{
public:
void printStudentInTeacher(Student &s);
};
void printStudent(Student s)
{
cout<<"outer function print:";
cout << s.grade << endl;
}
void Teacher::printStudentInTeacher(Student &s)
{
cout <<"Teacher print:";
cout << s.grade << endl;
} 展开
#include <iostream>
using namespace std;
class Teacher; //这里编译不成功,提示incomplete type 'Teacher' named in nested name specifier
class Student
{
friend void printStudent(Student grade);
friend void Teacher::printStudentInTeacher(Student &s);
public:
private:
int grade;
};
class Teacher
{
public:
void printStudentInTeacher(Student &s);
};
void printStudent(Student s)
{
cout<<"outer function print:";
cout << s.grade << endl;
}
void Teacher::printStudentInTeacher(Student &s)
{
cout <<"Teacher print:";
cout << s.grade << endl;
} 展开
3个回答
展开全部
顺序应该是这样:
//class Teacher; //这里编译不成功,提示incomplete type 'Teacher' named in nested name specifier
class Student;//add
class Teacher
{
public:
void printStudentInTeacher(Student &s);
};
class Student
{
friend void printStudent(Student grade);
friend void Teacher::printStudentInTeacher(Student &s);
public:
private:
int grade;
};
void printStudent(Student s)
{
cout << "outer function print:";
cout << s.grade << endl;
}
void Teacher::printStudentInTeacher(Student &s)
{
cout << "Teacher print:";
cout << s.grade << endl;
}
展开全部
worker(){}默认构造函数,其作用是把类的私有成员变量初始化为string类的默认值。
也就是空字符串。
~worker(){}是析构函数,其作用是在超出对象的作用域后,将对象所占的内存给释放掉。
避免物理内存被用尽
也就是空字符串。
~worker(){}是析构函数,其作用是在超出对象的作用域后,将对象所占的内存给释放掉。
避免物理内存被用尽
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
using namespace std;
class Student;
class Teacher
{
public:
void printStudentInTeacher(Student &s);
};
class Student
{
//public:
friend void printStudent(Student grade);
friend void Teacher::printStudentInTeacher(Student &s);
private:
int grade;
};
void printStudent(Student s)
{
cout<<"outer function print:";
cout << s.grade << endl;
}
void Teacher::printStudentInTeacher(Student &s)
{
cout <<"Teacher print:";
cout << s.grade << endl;
}
顺序应该是这样子的
using namespace std;
class Student;
class Teacher
{
public:
void printStudentInTeacher(Student &s);
};
class Student
{
//public:
friend void printStudent(Student grade);
friend void Teacher::printStudentInTeacher(Student &s);
private:
int grade;
};
void printStudent(Student s)
{
cout<<"outer function print:";
cout << s.grade << endl;
}
void Teacher::printStudentInTeacher(Student &s)
{
cout <<"Teacher print:";
cout << s.grade << endl;
}
顺序应该是这样子的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询