Inheritance constructors

I have two classes, Primary and Secondary. Secondary inherits everything from Primary, because they both do almost exactly the same thing. The constructor for Primary is as follows (needless to say, the prototype and the implementation are in separate files):

1
2
3
4
5
6
  Primary(unsigned int i);        //prototype

  Primary::Primary(unsigned int i)   //implementation
  {
    hash = new int[i];
  }


And the Secondary constructor is:

1
2
3
4
5
6
  Secondary(unsigned int i);       //prototype

  Secondary::Secondary(unsigned int i)     //implementation
  {
    hash = new int[i];
  }


The Primary class compiles fine, but Secondary compiles with the following error:

Secondary.cpp: In constructor ‘Secondary::Secondary(unsigned int)’:
Secondary.cpp:13: error: no matching function for call to ‘Primary::Primary()’
Primary.h:22: note: candidates are: Primary::Primary(unsigned int)
Primary.h:19: note: Primary::Primary(const PrimaryHash&)

I looked up this problem and found what I thought was a solution. I changed the implementation of the Secondary constructor to:

1
2
3
Primary(unsigned int size) : Secondary(size)
{
}


But this doesn't work either, giving the error:

Secondary.cpp:17: error: expected unqualified-id before ‘unsigned’
Secondary.cpp:17: error: expected ‘)’ before ‘unsigned’

As if it doesn't recognize Primary. How are you supposed to declare the derived class constructor in a case like this?



Last edited on
1
2
3
Secondary::Secondary(unsigned int size): Primary(size)
{
}
Thank you!
Topic archived. No new replies allowed.