【发布时间】:2017-08-21 16:42:45
【问题描述】:
我试图在另一个对象中创建一个对象数组(特别是在Checkbook 中的Check 数组。我不允许使用向量代替动态数组来存储对象,这令人抓狂,但这些都是我规定的规则。
我正在努力解决的问题是我需要为check 构造函数提供几个变量,因为它需要在check 对象中构造一个Money 对象。 所以我收到一条错误消息,声称 Check* checkArray = new Check[]; 没有默认构造函数。
我已经添加了一个默认构造函数,它只是 Check::Check() {};,但是如何在创建时不将参数最初传递给构造函数的情况下动态填充数组呢?我对 OOP 非常陌生,并且正在努力管理班级内的班级。 注意:money 类是预定义和实现的
这些相关支票的数据以 int(check number) '\t' double(check amount) '\t' int(bool for cashed 以 0 或 1 表示) 的形式存储在 .txt 文件中,并且我将数据临时存储在struct DataTransferStruct 中,然后将结构存储在向量中以进行测试,但我不能在最终实现中使用向量。我是不是在处理这个问题?
相关代码如下:
class Money
{
private:
long all_cents;
public:
friend Money operator +(const Money& amount1, const Money& amount2); //Returns sum of the values of amount1 and amount2
friend Money operator -(const Money& amount1, const Money& amount2); //Returns amount1 minus amount2
friend Money operator -(const Money& amount); //Returns the negative of the value of amount
friend bool operator ==(const Money& amount1, const Money& amount2); //Returns true if amount1 and amount2 have the same value; false otherwise
friend bool operator <(const Money& amount1, const Money& amount2) { return (amount1.all_cents < amount2.all_cents); }; //Returns true if amount1 is less than amount2; false otherwise
Money(long dollars, int cents); // Initializes the object so its value represents an amount with the dollars and cents given by arguments. If the amount is
//negative, then both dollars and cents should be negative
Money::Money(long dollars) : all_cents(dollars * 100) {} //Initializes the object so its value represents $dollars.00
Money::Money() : all_cents(0) {}//Initializes the object so its value represnets $0.00
double get_value() const; //Returns the amount of money recorded in the data portion of hte calling object
};
检查类
class Check
{
//Check dataTypes
private:
int checkNum;
Money checkAmount;
bool cashed;
public:
//Constructor
Check::Check(long dollar_Value, int cents_Value, int check_Num, int cashed_) : checkAmount(CreateMoneyClass(dollar_Value, cents_Value)) { checkNum = check_Num; if (cashed_ == 1)cashed = true; else cashed = false; };
Check::Check() {};
//Deconstructor
Check::~Check() {};
//Member functions
Money CreateMoneyClass(long dollar_Value, int cents_Value);
int GetCheckNum() const { return checkNum; };
double GetCheckAmount() const { return checkAmount.get_value(); };
bool CheckCashed() const { return cashed; };
};
Money Check::CreateMoneyClass(long dollar_Value, int cents_Value)
{
//Function that creates a Money object and returns to checkAmount within Check class
Money temp(dollar_Value,cents_Value);
return temp;
}
刚开始 CheckBook 课
class CheckBook
{
//Checkbook contains an array of checks
private:
Check* checkArray = new Check[];
};
我用来存储信息的方法
NewFile_Open(newFile);
//take in and format each data line w/ struct and then construct check in dynamic growing array
while (newFile >> temp.checkID)
{
newFile >> temp.rawMoneyAmount;
newFile >> temp.checkCashed;
//set dollars = rawMoneyAmount which drops cents
temp.dollars = temp.rawMoneyAmount;
//Get cents as int
temp.cents = (int)((temp.rawMoneyAmount - temp.dollars) * 100);
}
【问题讨论】:
-
选择一个“规则”不那么愚蠢的雇主?
-
@KerrekSB 哈哈不错
-
那么,你必须自己重新实现
std::vector。您需要分配适当大小和对齐的原始存储,然后根据需要使用placement-new 在其中构造您的对象。 -
@Quentin 我不允许使用矢量。我知道如何创建一个动态数组并相应地调整它的大小,但我不确定如何在类结构中做到这一点。
-
@StormsEdge 是的,这就是“重新实现
std::vector”的意思:创建自己的版本。
标签: c++ arrays oop constructor