「已注销」 2016-04-24 15:00 采纳率: 0%
浏览 3712

C++使用类的成员函数作为友元函数

题目是:定义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 实验三

请问哪里不对呢,那两个类该怎么声明和引用啊?

  • 写回答

9条回答 默认 最新

  • YXTS122 2016-04-24 15:24
    关注

    ifndef STUDENT.H #define STUDENT.H

    end if加上这些

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名