如何在C++中创建对象数组
How to create an array of objects in C++?
我是C++的新手。我有Java背景。我有两个类,分别叫SalesPerson
和SalesPeople
。
我想在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中,非基元对象(无论是在数组中,还是绑定到变量)实际上是指向内存中对象的引用。实际对象对程序员是隐藏的,只能通过引用访问。Java引用可以设置为null
,这意味着它不指向任何对象。这就是新创建的数组中所有引用的设置,也是为什么不需要默认构造函数来定义Java中的对象数组的原因。
在c++中,没有任何值是引用或指针(除非类型本身是引用/指针)。只有指针可以具有nullptr
值。一旦分配了一个数组,就会构造里面的所有对象。在c++中,指针数组的行为更像Java中的对象数组,不需要指针类型是默认可构造的,但我建议使用对象数组,除非您需要继承公共基的同级类型的对象数组。
您可能需要考虑使用一堆默认初始化的对象,而不是用参数构造的对象填充空的std::vector
,这是否有意义。
如果您在定义数组时知道参数,则也可以有一个非默认可构造对象数组:Type varname[size]{{PARAMS0}, {PARAMS1}, ...};
,其中ParamsX
是新构造对象的参数列表。
- C++创建对象数组
- 编译时生成应在构造函数中创建的非 constexpr 对象数组
- 使用 C++ 创建对象数组
- 无法将动态创建的对象数组从 qml 发送到 c++ 作为方法参数
- 在递归函数中更有效地创建对象和对象数组?C++
- 在对象中创建对象数组时的默认构造函数
- 如何使用安置新操作员创建对象数组
- 创建对象数组c++时崩溃
- 如何在C 中创建对象数组或类数组
- 创建对象数组会导致问题
- 在 NS-3 中创建对象数组时出错 (C++)
- 在程序函数中创建对象数组
- 当我尝试创建对象数组时出错
- 无法在 c++ 中创建对象数组
- 如何在C++中创建对象数组
- 创建对象数组然后有效删除它们的更好方法
- 创建对象数组时出现分段错误
- 创建对象数组c++
- 如何在没有动态内存分配的情况下创建对象数组
- 是否可以在C++中创建对象数组队列