题目是:定义Student类和Score类,输出一个学生的成绩单(包括学号、姓名、高数、英语、政治、C++成绩)要求使用Student成员函数作为友元函数
我是这样写的(用的是VS2013):
Student.h
#pragma once
#include<string>
using namespace std;
class Student
{
public:
Student(string num, string name)
{
this->num = num;
this->name = name;
}
~Student();
void ShowData(Score &);
private:
string num;
string name;
};
Student.cpp
#include "stdafx.h"
#include "Student.h"
#include "Score.h"
#include <iostream>
using namespace std;
Student::~Student()
{
}
void Student::ShowData(Score &stu)
{
cout << "=====学生信息=====\n";
cout << "学号:\t" << num;
cout << "\n姓名:\t" << name;
cout << "\n高数:\t" << stu.Math;
cout << "\n英语:\t" << stu.English;
cout << "\n政治:\t" << stu.Politics;
cout << "\nC++:\t" << stu.Cplusplus;
cout << "\n===================\n";
}
Score.h
#pragma once
class Student;
class Score
{
public:
Score(float Math, float English, float Politics, float Cplusplus)
{
this->Math = Math;
this->English = English;
this->Politics = Politics;
this->Cplusplus = Cplusplus;
}
~Score();
friend void Student::ShowData(Score &);
private:
float Math;
float English;
float Politics;
float Cplusplus;
};
Score.cpp
#include "stdafx.h"
#include "Score.h"
Score::~Score()
{
}
实验三.cpp
#include "stdafx.h"
#include "Score.h"
#include "Student.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string num, name;
float math, english, politics, cpluslus;
cout<<"请输入学号,姓名以及数学,英语,政治和C++成绩:\n";
cin >> num >> name >> math >> english >> politics >> cpluslus;
Student stu(num,name);
Score stu1(math,english,politics,cpluslus);
stu.ShowData(stu1);
return 0;
}
但是在Student.cpp文件中的ShowData函数中引用Score的数据成员时显示错误
然后调试的时候编译器就提示有下面的错误:
错误 1 error C2027: 使用了未定义类型“Student” c:\users\xxxx\documents\visual studio 2013\projects\实验三\实验三\score.h 17 1 实验三
错误 2 error C2061: 语法错误: 标识符“Score” c:\users\xxxx\documents\visual studio 2013\projects\实验三\实验三\student.h 16 1 实验三
错误 3 error C2245: 将不存在的成员函数“Student::ShowData”指定为友元(成员函数签名与所有重载都不匹配) c:\users\xxxx\documents\visual studio 2013\proje
cts\实验三\实验三\score.h 17 1 实验三错误 4 error C2511: “void Student::ShowData(Score &)”:“Student”中没有找到重载的成员函数 c:\users\xxxx\documents\visual studio 2013\projects\实验三\实验三\student.cpp 12 1 实验三
错误 5 error C2597: 对非静态成员“Student::num”的非法引用 c:\users\xxxx\documents\visual studio 2013\projects\实验三\实验三\student.cpp 14 1 实验三
错误 6 error C2597: 对非静态成员“Student::name”的非法引用 c:\users\xxxx\documents\visual studio 2013\projects\实验三\实验三\student.cpp 15 1 实验三
错误 7 error C2248: “Score::Math”: 无法访问 private 成员(在“Score”类中声明) c:\users\xxxx\documents\visual studio 2013\projects\实验三\实验三\student.cpp 16 1 实验三
错误 8 error C2248: “Score::English”: 无法访问 private 成员(在“Score”类中声明) c:\users\xxxx\documents\visual studio 2013\projects\实验三\实验三\student.cpp 17 1 实验三
错误 9 error C2248: “Score::Politics”: 无法访问 private 成员(在“Score”类中声明) c:\users\xxxx\documents\visual studio 2013\projects\实验三\实验三\student.cpp 18 1 实验三
错误 10 error C2248: “Score::Cplusplus”: 无法访问 private 成员(在“Score”类中声明) c:\users\xxxx\documents\visual studio 2013\projects\实验三\实验三\student.cpp 19 1 实验三
错误 11 error C2027: 使用了未定义类型“Student” c:\users\xxxx\documents\visual studio 2013\projects\实验三\实验三\score.h 17 1 实验三
12 IntelliSense: 不允许使用不完整的类型 c:\Users\xxxx\Documents\Visual Studio 2013\Projects\实验三\实验三\Score.h 17 14 实验三
请问哪里不对呢,那两个类该怎么声明和引用啊?