首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

C++ 類別編譯錯誤大全解析:缺少回傳型別、多餘分號、缺漏標頭檔

     編輯:程式設計疑難解答

錯誤清單與修正

錯誤訊息原因修正
error C2447: missing function headerint 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;
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved