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

How to create an array of objects in C++?

本文关键字:创建对象 数组 C++      更新时间:2023-10-16

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

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

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

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