0

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++?

6
  • 1
    By creating an array of type MyObject with MyObject arr[10], you are creating an array of MyObjects. C++ uses values extensively. Just like creating an int arr[10] in Java would actually create 10 ints. Commented Sep 23, 2014 at 7:24
  • 2
    Your first error (both cases) relates to the default constructor for SalesPeople, there isn't one. Either create one appropriately, or create a default constructor for SalesPerson so that the compiler generated one for SalesPeople is well defined. Simply adding SalesPerson() {} to SalesPerson will get it to compile, but it may need more work for what you want. Commented Sep 23, 2014 at 7:31
  • 4
    Are you required to use arrays? Is this an exercise in learning about arrays? If not, using std::vector may be a better idea. Commented Sep 23, 2014 at 7:33
  • @Niall This is an exercise to learn arrays and classes. I think I cant user vector Commented Sep 23, 2014 at 8:24
  • 1
    I recommend reading up on "the rule of three" and the follow ups "rule of zero" and "rule of five" to get a better understanding of the special member functions associated with a C++ class. Commented Sep 23, 2014 at 8:34

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

After adding a default constructor in SalesPerson It compiled properly. But why should I give a default constructor in SalesPerson? I have never seen such things in Java. Is there any special reason or this is the right way?
If I recall correctly, Java has no value semantics for non-primitive types. In that language arrays of objects are actually arrays of references to objects and the references are initialized to null. That is why you don't need a default constructor for the type to define an array of objects in Java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.