I have code of the following style:
class SubClass;
class SuperClass;
class SuperClass {
private:
void bar() {
SubClass().foo();
}
};
class SubClass : SuperClass {
public:
void foo() {};
};
So basically I have a SuperClass from where I want to call a method foo() of the subclass. VS 2012 gives me the following errors:
Error 1 error C2514: 'SubClass' : class has no constructors.
Error 2 error C2228: left of '.foo' must have class/struct/union.
What is the correct structure for what I want to do?
You can't do this. You must (at least) declare the method in the base class. For example:
#include <iostream>
class SuperClass
{
public:
void bar()
{
foo();
}
private:
virtual void foo() // could be pure virtual, if you like
{
std::cout << "SuperClass::foo()" << std::endl;
}
};
class SubClass : public SuperClass // do not forget to inherit public
{
public:
virtual void foo() { std::cout << "SubClass::foo()" << std::endl; }
};
int main()
{
SuperClass* pTest = new SubClass;
pTest -> bar();
delete pTest;
}
will print SubClass::foo()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With