我的教授給了我們這門課,並告訴我們它不會編譯。他表示捐助者陣列會與構造者發生衝突。所以...爲什麼會這樣呢?這個類的C++聲明有什麼問題?
我認爲Donor
陣列的名稱可能做到這一點,但由於成員數組donor
的名稱是區分大小寫,因此比類名不同,應該不是問題。
下面的代碼:
#ifndef DONORS_H
#define DONORS_H
#include <string>
#include "name.h"
#include "donor.h"
using namespace std;
const int
DONORS_LOAD_ERROR = 1,
DONORS_UPDATE_ERROR = 2,
DONORS_ADD_ERROR = 3;
const int MAX_DONORS = 100;
class Donors {
public:
Donors() : size(0) {}
void load(string filename);
int getSize() {return size;}
int find(Name name);
int add(Name name);
int add(Name name, Donation donation, int ytd);
void processDonation(Name name, Donation donation);
void update(string filename);
void print();
private:
Donor donorsList[MAX_DONORS];
int size;
};
#endif
教授寫道:
在這個版本中,我們已經採取了第2版,添加構造函數,並最大限度地利用對象。
然而,引入構造函數會破壞Donors類中數組數據成員的聲明; 因此這個版本不編譯!
我一直在和同學討論這件事,我們都很難過。這個C++類是什麼?
編輯:
編譯器的消息如下:
它只是發生在我身上的捐贈類有一個構造函數。因爲我們沒有觸及十腳的矢量,我們究竟應該怎樣編譯這個?
EDIT2:
這裏的施主類:
#ifndef DONOR_H
#define DONOR_H
#include "name.h"
#include "donation.h"
using namespace std;
class Donor {
public:
Donor(Name n, Donation ld=Donation(0, 0), int y=0) : name(n), lastDonation(ld), ytd(y) {}
Name getName() {return name;}
Donation getLastDonation() {return lastDonation;}
int getYtd() {return ytd;}
void processDonation(Donation d);
private:
Name name;
Donation lastDonation;
int ytd;
};
#endif
「不會編譯」 是不夠的錯誤描述,你知道的。 – Griwes 2012-03-19 17:17:10
當你試圖編譯它時,你得到了什麼錯誤信息? – 2012-03-19 17:17:32
編譯錯誤是什麼? – octal9 2012-03-19 17:17:39