1

Assume the following:

  • We have class Person, and class Job.

    class Job {
    public: 
        Job(); //default constructor
        Job(char * n); //parameterized constructor
        ~Job(); //destructor
    protected:
        Salary p; //another class
    }    
    
    class Person {
    public:
         Person(); //default constructor
         ~Person(); //destructor
         void set_job_name(char * n); //pass the job name to `Job` constructor
    
    protected:
         Job obj;
    };
    

When I try to use set_job_name(char * n) to call the parameterized constructor for class Job, I get this error Type "Job" does not provide a call operator. Any ideas?

This is what set_job_name() do:

void Person::set_job_name(char * n) {
    obj(n);
}
Prall1988
  • 31
  • 1
  • 4
  • In this case `obj(n)` is not a call to the `Job(char* n)` constructor. It is a call to `Job::operator()(char* n)` (which does not exist). You might want to take a brief C++ refresher: [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ivan Aksamentov - Drop Apr 10 '16 at 03:37
  • The very name `Person::set_job_name` smells fishy. Why not `Person::set_job`? – n. m. could be an AI Apr 10 '16 at 04:29

1 Answers1

2

The line

obj(n) 

is equivalent to:

obj.operator()(n) 

That is not right since Job which does not have an operator() function.

To reset the value of obj, use:

void Person::set_job_name(char * n) 
{
   obj = Job(n);
}

You can simplify it a bit by providing a set_name function in Job. If you do that, the above can be:

void Person::set_job_name(char * n) 
{
   obj.set_name(n);
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I did that `obj = Job(n);` but I get a seg-fault in the destructor of another class inside `Job`! I didn't add that class in my question, I will add it now – Prall1988 Apr 10 '16 at 04:13
  • @Prall1988, you should take at [The Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). That will be helpful if you allocate memory in the constructor and deallocate memory in the destructor. – R Sahu Apr 10 '16 at 04:19