我是這樣寫的(用的是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\projects\實驗三\實驗三\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 實驗三
請問哪里不對啊?那兩個類該怎么宣告啊?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/80128.html
標籤:基礎類
上一篇:[C++ Error] Unit1.cpp(10): E2268 Call to undefined function 'Printf'
下一篇:使用小波工具箱壓縮圖片
