我是C++新手。我來自Java背景。我有兩個類叫SalesPerson
和SalesPeople
。如何在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++中創建用戶定義對象的數組?
通過用'MyObject arr [10]'創建一個類型爲'MyObject'的數組,您可以創建一個'MyObject'數組。 C++廣泛使用_values_。就像在Java中創建一個int arr [10]一樣,實際上會創建10個整數。 – 2014-09-23 07:24:53
你的第一個錯誤(兩種情況)都涉及'SalesPeople'的默認構造函數,沒有一個。或者適當地創建一個,或者爲'SalesPerson'創建一個默認構造函數,以便編譯器爲'SalesPeople'生成一個已經定義好的。只需將'SalesPerson(){}'添加到'SalesPerson'即可編譯,但它可能需要更多的工作來滿足您的需求。 – Niall 2014-09-23 07:31:43
你需要使用數組嗎?這是學習陣列的練習嗎?如果不是,使用'std :: vector'可能是一個更好的主意。 – Niall 2014-09-23 07:33:41