2014-09-23 393 views
0

我是C++新手。我来自Java背景。我有两个类叫SalesPersonSalesPeople如何在C++中创建对象数组?

我想在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++中创建用户定义对象的数组?

+1

通过用'MyObject arr [10]'创建一个类型为'MyObject'的数组,您可以创建一个'MyObject'数组。 C++广泛使用_values_。就像在Java中创建一个int arr [10]一样,实际上会创建10个整数。 – 2014-09-23 07:24:53

+2

你的第一个错误(两种情况)都涉及'SalesPeople'的默认构造函数,没有一个。或者适当地创建一个,或者为'SalesPerson'创建一个默认构造函数,以便编译器为'SalesPeople'生成一个已经定义好的。只需将'SalesPerson(){}'添加到'SalesPerson'即可编译,但它可能需要更多的工作来满足您的需求。 – Niall 2014-09-23 07:31:43

+4

你需要使用数组吗?这是学习阵列的练习吗?如果不是,使用'std :: vector'可能是一个更好的主意。 – Niall 2014-09-23 07:33:41

回答

4

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

您似乎已经正确解释了错误,但您大概只会将默认构造函数设为SalesPeople。该类包含一个数组SalesPerson,所以如果你不给SalesPerson一个默认的构造函数,你也会遇到同样的问题。

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

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

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

可能有非默认constructible对象的数组,以及如果你知道当时的参数时,该数组定义:Type varname[size]{{PARAMS0}, {PARAMS1}, ...};其中ParamsX是新构造的对象的参数列表。

+0

在SalesPerson中添加默认构造函数之后,它编译正确。但为什么我应该在SalesPerson中提供默认构造函数?我从未在Java中看到过这样的事情。有没有什么特别的理由,或者这是正确的方法? – 2014-09-23 08:31:10

+1

如果我没有记错,Java没有非基元类型的值语义。在这种语言中,对象数组实际上是对对象引用的数组,并且引用被初始化为“null”。这就是为什么你不需要该类型的默认构造函数来定义Java中的对象数组。 – user2079303 2014-09-23 08:45:46