How do I put an object in a node if the object has no default constructor and I am not allowed to create one inside its class definition?

I'd like to point out right away that I have already searched the site and haven't found a similar problem. There have been questions about someone not being able to put an object in a node but my problem is this:

I cannot put an object inside a Node AND I am not allowed to create a default constructor for the object (at least not inside its class definition)

More specifically:

I have been given following class Employer:

class Employer
{    
public:

    Employer(string x, int a, int b, int c);

    string getname() const { return name; }

private:
    string name;
    int age, sal, hire;
};

Employer::Employer(string x, int a, int b, int c) : name(x), age(a), sal(b), hire(c) { }

As you can see there is no default constructor for this class and I am not allowed to create one (these are the rules..!)

And I have created the following class Node:

class Node
{
public:
    Employer emp;
    Node* link;
    Node(Employer);
};

Node::Node(Employer x)
{
    emp= x;
    link=NULL;
}

as you can see I want to create a Node class that holds a node pointer and an object Employer inside it.

Unfortunately this isn't working. I keep getting the following error message:

E2279: Cannot find default constructor to initialize member.....

Basically what happens is because class Employer has no default constructor, "Employer emp;" in the Node class is not "recognised" by the compiler as it would have been if it was a "int n" instead of a "Employer emp".

So my question is this:

How can I put the object Employer in the Node without changing/adding anything to the class Employer (ie I cannot add a default constructor to the class Employer nor can I add a "friend class Node" kind of thing, the rules are I should leave the class Employer as it is)

Since I am new to programming I'd like to add that I haven't been taught templates yet, so I am not allowed to use any of that, just the tools I've already given you (classes,functions,pointers..etc)

Answers


Provide an explicit constructor call for the Employer member in the initializer list:

class Node
{
public:
    Employer emp;
    Node* link;
    Node(Employer e) : emp(e) { }
};

In this example, I chose to use the copy constructor, which you didn't explicitly define for Employer, but which is there anyway. Typically, you would prefer to take the argument by const-reference:

Node::Node(Employer const & e) : emp(e), link(NULL) { }

You can also provide additional constructors, for example:

Node::Node(std::string const & s, int d) : emp(s, 12, d, 24), link(NULL) { }

(The less you put in your constructor body the better.)


Need Your Help

Sorting varchar column as numeric in mysql with null value as the bottom

mysql sorting numeric varchar

i am trying to sort a column in the mysql query which is of type varchar but has may/maynot have numeric values.

How to get a tomcat Environment variable in a PropertyPlaceholderConfigurer

java hibernate spring

I try to get an Environment variable specified in Tomcat's server.xml in a 'PropertyPlaceholderConfigurer' located in my jpa-spring.xml file.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.