2014-09-23 29 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