I’ve worked a fair amount with singleton classes in C++ (for a class), and now for a not-class project, I’m attempting to use inherited classes.
For some reason, I’m being really thick about constructor chains.
Consider the following:
class SuperClass
{
string privateKey;
string publicKey;
string name;
public:
enum Key {PUBLIC, PRIVATE};
SuperClass(Key, string);
};
SuperClass::SuperClass(Key key, string theKey)
{
switch(key)
{
case PUBLIC: publicKey = theKey;
break;
case PRIVATE: privateKey = theKey;
break;
}
}
class SubClass : private SuperClass
{
public:
SubClass(string key);
};
SubClass::SubClass(string key)
: SuperClass(SuperClass::PUBLIC, key) // "SubClass" will only interact with the public key, in this case.
{
cout << "I'm instantiated!" << endl;
}
int main()
{
SubClass example ("thisIsThePublicKey");
return 0;
}
When compiling, I get the following error:
error: no matching function ncall to SuperClass::SuperClass()
note: candidates are: SuperClass::SuperClass(SuperClass::Key, std::string)
Why do I get this? Haven’t I used the constructor it says is a candidate?
I feel like I’ve read every tutorial on constructor chains, and I still don’t get what I’m supposed to be doing differently.
Thanks for any help!
I believe you forgot to inherit from your super class:
Should be: