我的教授给了我们这门课,并告诉我们它不会编译。他表示捐助者阵列会与构造者发生冲突。所以...为什么会这样呢?这个类的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