我试图在另一个对象中创建一个对象数组(特别是Check
中的Checkbook
数组。我不允许使用向量代替动态数组来存储对象这令人抓狂,但这些是我已经规定的规则。
我正在努力解决的问题是我需要为check
构造函数提供几个变量,因为它需要在Money
对象中构造一个check
对象。 所以我收到一条错误,声称Check* checkArray = new Check[];
没有默认构造函数。
我已经添加了一个只有Check::Check() {};
的默认构造函数,但是如何在创建时没有参数最初传递给构造函数的情况下动态填充数组?我是OOP的新手,我正在努力管理课程中的课程。 注意:资金类是预先定义并实施的
这些相关性检查的数据以int(检查编号)'\ t'double(检查金额)'\ t'int的形式存储在.txt文件中(bool表示以0或1表示的兑现)和我暂时将数据存储在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);
}
答案 0 :(得分:2)
你不应该在类
中创建这样的数组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];
}
};
答案 1 :(得分:2)
构建没有默认构造函数的对象数组的正确方法是使用placement new
语法来分隔数组的分配和对象的构造。
Checkbook::CheckBook(int size) { // A Checkbook containing size Checks
// first the allocation
Check * checkArray = reinterpret_cast<Check *>(new char[size * sizeof(Check)]);
// then the actual construction
for (int i=0; i<size; i++) {
int checkNum = ...; // compute a check number possibly depending on i
//actual construction of checkArray[i]
new(checkArray + i) Check(0., 0., checkNumber, false);
}
}
答案 2 :(得分:1)
一个问题是您正在尝试在类定义中初始化checkArray对象。这不适合这样做。相反,checkArray类型只应在此处声明,并在CheckBook的构造函数中初始化:
class CheckBook
{
public:
CheckBook()
: checkArray(new Check[]) // still an error here!
{}
private:
Check* checkArray;
};
然而,这仍然不正确,因为C数组是固定大小,这是一个大问题:您需要提前知道大小并将其传递给构造函数:
class CheckBook
{
public:
CheckBook(unsigned int numChecks)
: checkArray(new Check[numChecks])
{}
private:
Check* checkArray;
};
除非这是一个家庭作业问题,否则你应该真正回到不允许std::vector
的短视要求。在这种情况下,数组可能不是正确的容器。 (我不会说这是家庭作业的机会会更好。)
答案 3 :(得分:0)
我建议制作一个单独的Init方法,并让所有构造函数和初始化通过它。这样,如果您以后需要CheckBooks的默认构造函数(例如,您需要一个CheckBook数组),那么转换它会更容易。
class CheckBook
{
public:
CheckBook(unsigned int numChecks) : checkArray(0), size(0)
{
Init(numChecks);
}
void Init(int _size)
{
if(checkArray != 0) // if there's already a checkArray, replace it.
{
delete [] checkArray;
}
size = _size;
checkArray = new Check[size];
// for completeness you'd want to check that checkArray is not null
// otherwise the memory allocation failed
for(int i = 0; i < size; ++i)
{
// do your per-check set-ups here
}
}
~CheckBook() // make sure to clean up the dynamic memory
{
delete [] checkArray;
}
private:
Check* checkArray;
int size;
};