【发布时间】:2012-04-04 04:55:47
【问题描述】:
我的教授给了我们这门课,并告诉我们它不会编译。他说捐助者数组会与构造函数发生冲突。所以……为什么会这样?
我认为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++ 类是怎么回事?
编辑:
编译器消息如下所示:
我突然想到 Donor 类有一个构造函数。既然我们还没有接触过十英尺长的向量,我们到底应该如何编译它?
编辑2:
这是捐助者类:
#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
【问题讨论】:
-
“不编译”是不够的错误描述,你知道的。
-
您在尝试编译时收到了什么错误信息?
-
编译错误是什么?
-
由于您没有提供错误消息,因此我投票决定关闭它,因为这不是一个真正的问题。
-
我们还需要看
Donor类的声明