Home > Software engineering >  How to properly declare a list in a class
How to properly declare a list in a class

Time:01-18

Context: I'm at the second year of Uni and we started using classes and understanding of creating some little applications. I'm building an employer class, every employer has 4 parameters: serial_number, name, surname, earnings and then there also junior and senior employers, with all the above but other parameters too. For example junior employers have a set of skills and intern: first one is a list of skills that a junior can have and the second is at which senior employer they are assigned. ex. Junior with serial_number3 is the intern of Senior with serial_number1

NOW: I make use of the employer.h for the easy stuff, but when I get into declaring the list skills I just didn't understand what I should do.

#ifndef JUNIOR_H
#define JUNIOR_H

#include "employer.h"

#include <list>
using namespace std;

class junior : public employer
{
public:
    junior(string serial, string name, string surname, double earns, list<string> sk, string in)
        : employer(serial, name, surname, earns), skills(sk), intern(in) {}

    list<string> getSkills() const { return skills; }
    string getIntern() const { return intern; }

private:
    list<string> skills;
    string intern;

};

#endif // JUNIOR_H

if I try to write this in the main, it gives me now "no matching constructor for initialization of junior" and I have included the header

junior j("serial3", "name1" , "sur1", 3931, "cpp", "serial1"); 

CodePudding user response:

The problem is, that the constructor expects as the 5th parameter a list<string>. You are just handing in a normal string "cpp".

You can simply add a braced initializer list as the 5th parameter. In your case, just with one string only: "cpp". This will create a list with one member "cpp".

I also recommend qualifying your parameter as const reference. This will avoid a lot of copying.

Then your program looks like the following and will compile.

#include <list>
#include <string>
using namespace std::string_literals;

using namespace std;

struct employer {
    string serial{};
    string name{};
    string surname{};
    double earns{};
    employer(const string& serialp, const string& namep, const string& surnamep, const double earnsp) :
        serial(serialp), name(namep), surname(surnamep), earns(earnsp) {};
};

class junior : public employer
{
public:
    junior(const string& serial, const string& name, const string& surname, const double earns, const list<string>& sk, const string& in)
        : employer(serial, name, surname, earns), skills(sk), intern(in) {}

    list<string> getSkills() const { return skills; }
    string getIntern() const { return intern; }

private:
    list<string> skills;
    string intern;

};
int main() {
    junior j("serial3"s, "name1"s, "sur1"s, 3931, { "cpp"s }, "serial1"s);
}

Using using namespace std; for small demo projects is ok. Later you should definitely avoid that.

I used also string_literals. With that "Hello World"swill be of type string

CodePudding user response:

simply write "junior("serial3", "name1" , "sur1", 3931, "cpp", "serial1");" not "junior j("serial3", "name1" , "sur1", 3931, "cpp", "serial1");".......why are u writing j with it???

  • Related