C ++ error compilation because private member

My main class Taskhas a private member:

private:
    Task();

I add a derivation class Schedulerwith a class expression Task:

class Scheduler : public Task {
    friend class Task;`

I create a file Scheduler.ccto implement the class derivation constructor Scheduler:

Scheduler::Scheduler() {
    //nothing here.
}

I am trying to compile with a constructor Schedulerin an empty field, but I got a compilation error that I do not understand the relationship because my constructor is Schedulerempty:

/tmp/PROJETO/T1/booos-t1/lib/Task.h: In constructor β€˜BOOOS::Scheduler::Scheduler()’: /tmp/PROJETO/T1/booos-t1/lib/Task.h:41:2: error: β€˜BOOOS::Task::Task()’ is private Scheduler.cc:13:22: error: within this context make[1]: ** [Scheduler.o] Erro 1

I would like to understand my problem because I am not trying to access the private member of the Task class in my constructor Scheduler.

+4
source share
3 answers

Task Scheduler,

Scheduler::Scheduler() {
  //nothing here.
}

Scheduler::Scheduler() : Task() {
  //nothing here.
}

Task::Task() , .

Task protected, . , Task , Task .

protected:
      Task();
+5

Task() private member, a private default constructor.

private. protected, , -, :

class Task
{
public:
    Task( int i );

private:
    Task();
};

Scheduler::Scheduler()
{ // does not compile, equivalent to the next one
}

Scheduler::Scheduler() : Task() 
{ // does not compile
}

Scheduler::Scheduler() : Task(3) 
{ // does compile!
}
+4

Because the constructor always invokes the constructor of the parent class. Either explicitly through the initialization list, or implicitly the default constructor. And here the default constructor of the parent class is private, so ...

+1
source

All Articles