Reputation: 2663
I am new to C++. I am from Java background. I have two classes called SalesPerson
and SalesPeople
.
I want to create an array of SalesPerson
in SalesPeople
class. Below is my program and I get some compilation errors.
#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;
}
Compilation errors:
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
Then I tried to create an empty constructor and I got the below error.
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
How can I create an array of user defined objects in C++?
Upvotes: 0
Views: 310
Reputation: 238311
You create arrays exactly as you are trying: Type varname[size];
. The instances in the array must be initialized though and since you don't provide any parameters, the type must have a default constructor, as the compilation error indicates.
You seem to have interpreted the error correctly, but you've presumably only given default constructor to SalesPeople
. That class contains an array of SalesPerson
, so you get the same problem if you don't give SalesPerson
a default constructor as well.
In Java, non-primitive objects are (whether in an array, or bound to a variable) actually references that point to objects in memory. The actual objects are hidden from the programmer and are only accessible through the references. Java references can be set to null
which means that it doesn't point to any object. That's what all the references in a newly created array are set to and it's why you don't need a default constructor to define an array of objects in Java.
In c++, no value is a reference or a pointer (unless the type itself is a reference/pointer). Only pointers can have nullptr
value. Once an array is allocated, all the objects inside are constructed. In c++ An array of pointers behaves much more like an array of objects in Java and doesn't require the pointed type to be default-constructible but I recommend using an array of objects unless you need to have an array of objects of sibling types that inherit a common base.
You might want to consider whether it makes sense to have a bunch of default initialized objects rather than an empty std::vector
that you fill with objects that are constructed with parameters.
It is possible to have an array of non-default-constructible objects as well if you know the parameters at the time when the array is defined: Type varname[size]{{PARAMS0}, {PARAMS1}, ...};
where ParamsX
are parameter lists for the newly constructed objects.
Upvotes: 4