gpt4 book ai didi

c++ - 如何在 C++ 中创建对象数组?

转载 作者:行者123 更新时间:2023-11-27 23:02:58 25 4
gpt4 key购买 nike

我是 C++ 新手。我来自 Java 背景。我有两个名为 SalesPersonSalesPeople 的类。

我想在 SalesPeople 类中创建一个 SalesPerson 数组。下面是我的程序,我遇到了一些编译错误。

#include <iostream>
using namespace std;

class SalesPerson {

private:
string name;
static const int MONTHS = 12;
double salesPerMonthArray[MONTHS];

public:
SalesPerson(string name, double salesPerMonthArray[]) {

this->name = name;

for(int i = 0; i < MONTHS; i++) {
this->salesPerMonthArray[i] = salesPerMonthArray[i];
}
}

string getName() {
return name;
}

double getSalesForAMonth(int month) {

if(month < MONTHS) {
return salesPerMonthArray[month];
}
return salesPerMonthArray[0];
}

double computeAnnualSales() {

double annualSales = 0.0;
for(int i = 0; i < MONTHS; i++) {
annualSales += salesPerMonthArray[i];
}

return annualSales;
}
};

class SalesPeople {

private:
int index = 0;
SalesPerson salesPersonArray[10];

public:

void addSalesPerson(SalesPerson salesPerson) {
salesPersonArray[index] = salesPerson;
index++;
}

SalesPerson getSalesPerson(int index) {
return salesPersonArray[index];
}
};

int main() {

double salesArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
SalesPeople salesPeople;

SalesPerson salesPerson1("yaswanth", salesArray);

salesPeople.addSalesPerson(salesPerson1);

return 0;
}

编译错误:

prog.cpp: In function ‘int main()’:
prog.cpp:65:14: error: use of deleted function ‘SalesPeople::SalesPeople()’
SalesPeople salesPeople;
^
prog.cpp:44:7: note: ‘SalesPeople::SalesPeople()’ is implicitly deleted because the default definition would be ill-formed:
class SalesPeople {
^
prog.cpp:44:7: error: no matching function for call to ‘SalesPerson::SalesPerson()’
prog.cpp:44:7: note: candidates are:
prog.cpp:12:2: note: SalesPerson::SalesPerson(std::string, double*)
SalesPerson(string name, double salesPerMonthArray[]) {
^
prog.cpp:12:2: note: candidate expects 2 arguments, 0 provided
prog.cpp:4:7: note: SalesPerson::SalesPerson(const SalesPerson&)
class SalesPerson {
^
prog.cpp:4:7: note: candidate expects 1 argument, 0 provided
prog.cpp:4:7: note: SalesPerson::SalesPerson(SalesPerson&&)
prog.cpp:4:7: note: candidate expects 1 argument, 0 provided

然后我尝试创建一个空的构造函数,但出现以下错误。

prog.cpp: In constructor ‘SalesPeople::SalesPeople()’:
prog.cpp:51:17: error: no matching function for call to ‘SalesPerson::SalesPerson()’
SalesPeople() {
^
prog.cpp:51:17: note: candidates are:
prog.cpp:12:2: note: SalesPerson::SalesPerson(std::string, double*)
SalesPerson(string name, double salesPerMonthArray[]) {
^
prog.cpp:12:2: note: candidate expects 2 arguments, 0 provided
prog.cpp:4:7: note: SalesPerson::SalesPerson(const SalesPerson&)
class SalesPerson {
^
prog.cpp:4:7: note: candidate expects 1 argument, 0 provided
prog.cpp:4:7: note: SalesPerson::SalesPerson(SalesPerson&&)
prog.cpp:4:7: note: candidate expects 1 argument, 0 provided
prog.cpp: In constructor ‘SalesPeople::SalesPeople()’:
prog.cpp:51:17: error: no matching function for call to ‘SalesPerson::SalesPerson()’
SalesPeople() {
^
prog.cpp:51:17: note: candidates are:
prog.cpp:12:2: note: SalesPerson::SalesPerson(std::string, double*)
SalesPerson(string name, double salesPerMonthArray[]) {
^
prog.cpp:12:2: note: candidate expects 2 arguments, 0 provided
prog.cpp:4:7: note: SalesPerson::SalesPerson(const SalesPerson&)
class SalesPerson {
^
prog.cpp:4:7: note: candidate expects 1 argument, 0 provided
prog.cpp:4:7: note: SalesPerson::SalesPerson(SalesPerson&&)
prog.cpp:4:7: note: candidate expects 1 argument, 0 provided

如何在 C++ 中创建用户定义对象数组?

最佳答案

您完全按照您的尝试创建数组:Type varname[size];。尽管必须初始化数组中的实例,并且由于您不提供任何参数,该类型必须具有默认构造函数,如编译错误所示。

您似乎正确地解释了错误,但您可能只将默认构造函数提供给了 SalesPeople。该类包含一个 SalesPerson 数组,因此如果您不给 SalesPerson 一个默认构造函数,您也会遇到同样的问题。

在 Java 中,非原始对象(无论是在数组中,还是绑定(bind)到变量)实际上是指向内存中对象的引用。实际对象对程序员是隐藏的,只能通过引用访问。 Java 引用可以设置为 null,这意味着它不指向任何对象。这就是新创建的数组中所有引用的设置,也是为什么您不需要默认构造函数来定义 Java 中的对象数组的原因。

在 C++ 中,没有值是引用或指针(除非类型本身是引用/指针)。只有指针可以有 nullptr 值。一旦分配了一个数组,就构造了其中的所有对象。在 C++ 中,指针数组的行为更像 Java 中的对象数组,并且不需要指向类型是默认可构造的,但我建议使用对象数组,除非您需要具有兄弟类型的对象数组继承一个共同的基础。

您可能想考虑拥有一堆默认初始化对象而不是空的 std::vector 是否有意义,您用参数构造的对象填充它。

如果您在定义数组时知道参数,那么也可以有一个非默认可构造对象数组:Type varname[size]{{ PARAMS0}, {PARAMS1}, ...}; 其中 ParamsX 是新构造对象的参数列表。

关于c++ - 如何在 C++ 中创建对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25989261/

25 4 0
文章推荐: C++ Getter 方法打印奇怪的数字
文章推荐: javascript -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com