我一直在這個任務上工作了一段時間。這裏的說明:C++:派生類,「沒有匹配的構造函數」錯誤
你要設計一個名爲僱員的成員 爲如下(讓他們保護)抽象類:
數據成員:字符*名稱,長整型ID
兩個構造函數:默認構造函數// intitialize數據memebrs到 默認值和一個拷貝構造函數
方法:setPerson(字符* N,長整型ID)//允許用戶設置 信息對於每個人一個名爲Print()的函數//應該是一個 虛函數,它打印該類的數據屬性。和一個 析構函數
另外定義派生自Employee類的兩個類,稱爲 Manager和Secretary。每個類都應繼承基類的所有成員,並擁有其自己的數據成員和成員函數。 經理應該有一個名爲學位的數據成員 本科學位(例如文憑,學士,碩士,博士), 祕書應該有她的合同(對於 永久/臨時可以是布爾值1/0) 。
派生類的所有成員函數都應該從其基類 中被忽略。
寫下面的main()來測試你的類
int main() { Employee * p = new Manager(「Bruce Lee」, 0234567, 「Dr.」); P.print(); Secretary p2; p2.setPerson(「Wilma Jones」, 0341256, 「permanent」); delete p; p = & p2; p.Print(); return 0; }
這一切我來了這麼遠,但我敢肯定它是錯漏百出,我的論點和變量類型全部關閉。
#include <iostream>
using namespace std;
class Employee{
protected:
char *name;
long int ID;
public:
Employee();
Employee(Employee&);
void setPerson(char * n, long int eID) {
name = n;
ID = eID; };
virtual void Print(){
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl; };
};
class Manager: public Employee {
protected:
char *degree;
public:
void setPerson(char * n, long int eID, char * d){
name = n;
ID = eID;
degree = d;
};
void Print() {
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl;
cout << "Degree: " << degree << endl;
};
};
class Secretary: public Employee {
protected:
bool contract;
public:
void setPerson(char * n, long int eID, string c){
name = n;
ID = eID;
if (c == "permanent") contract = true;
else contract = false;
};
void Print(){
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl;
cout << "Contract: " << contract << endl;
};
};
int main() {
Employee * P = new Manager("Bruce Lee", 0234567, "Dr.");
P.Print();
Secretary P2;
P2.setPerson("Wilma Jones", 0341256, "permanent");
delete P;
P = & P2;
P.Print();
return 0;
}
我得到上線62個錯誤(主代碼的第一行):
沒有匹配的構造器管理器初始化
我試着閱讀類似的問題,但他們沒有幫助我很多。我認爲最容易混淆的是合約是一個bool值和使用char參數。任何指導都值得讚賞。
第57行和第62行出現「P.Print()」錯誤,「不提供你提到的錯誤。他們更可能給出'左'.Print'必須有class/struct/union'錯誤信息。 – 2016-04-21 19:11:53
@RawN我的第62行不同於這裏的第62行(因爲Xcode在開始的時候添加了註釋)。這就是爲什麼我指定它是主代碼的第一行,但是你的正確性在於打印函數不是工作。我沒有提到它,因爲我認識到它是Manager未初始化時的錯誤。 – hopillium