2

I am trying to make a program which creates an array of pointers to objects, including inherited objects. The error I'm getting is on the first bracket ( of the cSClass eg SArray[2] = new cSClass(...); (Last line at the bottom of all the code). The error says "no instance of constructor cSClass::cSClass matches the argument list"

Thank you

All the code is below:

The code for the header of the superclass is:

class sClass;

class sClass {
protected:
    std::string name;
    int yearBuilt;
public:
    // Constructor
    sClass(string n = "s1", int yb = 2000) {
        name = n;
        yearBuilt = yb;
    }
// Mutator functions
void setName(string);
void setYearBuilt(int);

// Accessor functions
string getName() {
    return name;
}
int getYearBuilt() {
    return yearBuilt;
}

// Functions
void getInfo();
};

main class of the superclass:

#include "sClass.h"
using namespace std;

// Mutators
void sClass::setName(string n) {
    name = n;
}
void sClass::setYearBuilt(int yb) {
    yearBuilt = yb;
}

// Print function
void sClass::getInfo() {
    cout << "Name: " << name << endl;
    cout << "Year Built: " << yearBuilt << endl;
}

Code for the subclass header:

#include "sClass.h"

class cSClass : public sClass {
protected:
    int maxPassengers;
public:
    // Constructor
    cSClass(int mp = 2000) : sClass() {
        maxPassengers = mp;
    }

    // Mutator functions
    void setMaxPassengers(int);

    // Accessor functions
    int getMaxPassengers() {
        return maxPassengers;
    }

    // Functions
    void getInfo() {
    }
};

Code for the subclass class: #include "cSClass.h"

// Mutators
void cSClass::setMaxPassengers(int mp) {
    maxPassengers = mp;
}

// Print function
void cSClass::getInfo() {
    cout << "Name: " << name << endl;
    cout << "Maximum  Passengers: " << maxPassengers << endl;
}

And lastly this is the main program code in which i am getting errors where i am trying to fill the array: #include "sClass.h" #include "cSClass.h"

int main() {
sClass *SArray[6];

    SArray[0] = new sClass(...);
    SArray[1] = new sClass(...);
    SArray[2] = new cSClass(...);
    SArray[3] = new cSClass(...);
}

Edit: Error is at the top, and the arguments I'm passing are

SArray[2] = new cSClass("RMS Queen Mary 2", 2003, 2700);
7
  • What errors? Paste them into the question. Commented Apr 15, 2020 at 17:07
  • new sClass(...) - What are the actual arguments you are passing here? This is important since the error is clearly complaining about the constructor arguments. Commented Apr 15, 2020 at 17:08
  • And are you literally typing ... in your constructors? Or is it supposed to values? The biggest issue is that your superclass does NOT have a default constructor, but you try calling it anyway in your subclass constructor. Commented Apr 15, 2020 at 17:09
  • 1) Is this Ship(string n = "s1", int yb = 2000) {...} sClass's constructor? (upd ok after post edition) 2) There is no 3-arguments cSClass constructor, only 1-argument: cSClass(int mp = 2000) {...}, pass two other arguments too. Commented Apr 15, 2020 at 17:10
  • Edit at the bottom of the post, also @TedLyngmo that was an error, Ship was supposed to be sClass too Commented Apr 15, 2020 at 17:13

2 Answers 2

1

The constructor needed for this to work is missing:

SArray[2] = new cSClass("RMS Queen Mary 2", 2003, 2700);

It could look like this

class cSClass : public sClass {
    cSClass(const std::string& name, int yb, int mp) :
        sClass(name, yb),
        maxPassengers{mp}
    {}
    //...
}

You also have some other problems:

  • You have a non-virtual destructor in the base class. When you delete your objects through a non-virtual base class pointer, only the base class destructor will be called. To fix that, add this to sClass:

    virtual ~sClass() = default;
    
  • Two definitions of cSClass::getInfo(). Settle for only declaring the function in the class definition and leave the definition of the member function in the .cpp file as-is.

  • Memory leaks since you don't delete what you've newed. To avoid that problem, it's better to use smart pointers that will delete the object when it goes out of scope, like it will in case an exception is thrown (that you catch). Example:

    #include <memory>
    //...
    std::unique_ptr<sClass> SArray[6]; // or std::array<std::unique_ptr<sClass>, 6> sArray;
    SArray[2] = std::make_unique<sClass>();
    SArray[2] = std::make_unique<cSClass>("RMS Queen Mary 2", 2003, 2700);
    

    Note: If you instead want a dynamic amount of sClass pointers, use a std::vector:

    #include <vector>
    //...
    std::vector<std::unique_ptr<sClass>> SArray;
    
    SArray.push_back(std::make_unique<sClass>());
    SArray.push_back(std::make_unique<sClass>());
    SArray.push_back(std::make_unique<cSClass>("RMS Queen Mary 2", 2003, 2700));
    SArray.push_back(std::make_unique<cSClass>("RMS Queen Mary 2", 2003, 2700));
    
Sign up to request clarification or add additional context in comments.

Comments

0

There are two fundamental errors in your code!

First, you have provided two definitions for the getInfo member function of the cSClass. If you want to keep the second (out-of-body) definition, then you need to remove the definition part of the (in-body) declaration. So, replace:

    // Functions
    void getInfo() { /// Note: adding the { } provides a function DEFINITION
    }

with this:

    // Functions
    void getInfo(); // No body provided, so it's JUST a declaration (defined elsewhere)

Then, the calls you make to your constructors cannot have the ... in the argument list (though I'm not sure what you are trying to achieve with this). Simply provide empty argument lists:

    SArray[0] = new sClass();
    SArray[1] = new sClass();
    SArray[2] = new cSClass();
    SArray[3] = new cSClass();

or, as there is no argument, you can invoke the 'default' constructor by omitting the argument lists completely:

    SArray[0] = new sClass;
    SArray[1] = new sClass;
    SArray[2] = new cSClass;
    SArray[3] = new cSClass;

Also, for completeness, remember to free the memory for the objects you created with new, when you're done with them:

    delete SArray[0];
    delete SArray[1];
    delete SArray[2];
    delete SArray[3];

Feel free to ask for further clarification and/or explanation.

Comments

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.