錯誤清單與修正
| 錯誤訊息 | 原因 | 修正 |
| error C2447: missing function header | int date(...); { 函數定義前多餘分號 | 移除分號:int date(...) { |
| error C2264: 'Set' : error in function definition | 成員函數定義缺少回傳型別 | 加上 void:void Stu::Set(...) |
| cout<<"name:"< } | 語句不完整、缺少 #include <iostream> 及 using namespace std; | 補全標頭、命名空間,修正輸出語句 |
| friend 函數宣告與定義不一致 | 類別內 friend int date(...); 定義時回傳 int 但未使用 | 統一簽名,或改為 void |
修正後完整範例
#include
using namespace std;
class Stu {
char *name;
double score;
public:
void Set(char *na, double s);
void Show();
~Stu();
friend void date(Stu &, Stu &, Stu &, Stu &, Stu &);
};
void Stu::Set(char *na, double s) {
score = s;
name = new char[strlen(na)+1];
strcpy(name, na);
}
void Stu::Show() { cout << "name:" << name << " score:" << score << endl; }
Stu::~Stu() { delete[] name; }
void date(Stu &s1, Stu &s2, Stu &s3, Stu &s4, Stu &s5) {
double a[5] = {s1.score, s2.score, s3.score, s4.score, s5.score};
sort(begin(a), end(a));
for (double v : a) cout << v << endl;
}
int main() {
Stu s1,s2,s3,s4,s5;
s1.Set("zhang",78); s1.Show();
s2.Set("wang",89); s2.Show();
s3.Set("zhao",92); s3.Show();
s4.Set("li",65); s4.Show();
s5.Set("chen",50); s5.Show();
date(s1,s2,s3,s4,s5);
return 0;
}