我是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