gpt4 book ai didi

c++ - 在对象中创建对象数组时的默认构造函数

转载 作者:搜寻专家 更新时间:2023-10-31 00:29:00 26 4
gpt4 key购买 nike

我正在尝试在另一个对象中创建对象数组(特别是 Checkbook 中的 Check 数组。我不允许使用 vector 代替动态数组来存储对象,这让人抓狂,但那些是已经规定给我的规则。

我遇到的问题是我需要为 check 构造函数提供几个变量,因为它需要在 check 中构造一个 Money 对象 对象。 所以我收到一条错误消息,声称 Check* checkArray = new Check[]; 没有默认构造函数。

此后我添加了一个默认构造函数,它只是 Check::Check() {};,但是我如何动态填充数组而不在创建时将参数最初传递给构造函数?我是 OOP 的新手,正在努力管理类(class)内的类(class)。 注意:money 类是预定义和实现的

这些相关支票的数据以 int(check number) '\t' double(check amount) '\t' int(bool for cashed in 0 or 1) 和我暂时将数据存储在 struct DataTransferStruct 中,然后将结构存储在 vector 中只是为了测试,但我无法在最终实现中使用该 vector 。我处理这个问题的方式不好吗?

相关代码如下:

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);
}

最佳答案

你不应该在类里面创建这样的数组

class CheckBook
{
//Checkbook contains an array of checks
private:
Check* checkArray = new Check[];

};

您应该在类 CheckBook 构造函数中定义它。

喜欢

class CheckBook
{
//Checkbook contains an array of checks
private:
Check* checkArray;
Checkbook()
{
checkArray = new checkArray[size];
}
};

关于c++ - 在对象中创建对象数组时的默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43072423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com