J
Jessica
I have a base class and a derived class, but I am getting errors when I
try to access functions of the derived class.
Simplified version of my code is as follows:
////////////////
// test2.hh
class BaseClass {
public:
BaseClass();
~BaseClass();
};
class DerivedClass : public BaseClass {
public:
DerivedClass() {}
~DerivedClass() {}
void setup();
};
////////////////
// test2.cc
#include "test2.hh"
int main() {
BaseClass* object;
object = new DerivedClass();
object->setup();
return 0;
}
When I try to compile this, I get the error:
test2.cc:8: error: `setup' undeclared (first use this function)
If I change the line to say:
object->DerivedClass::setup();
I get the error:
test2.cc:8: error: type `DerivedClass' is not a base type for type
`BaseClass'
And if I change the pointer declaration to say:
DerivedClass* object;
I get the linker error:
/tmp/ccaPvY8l.o: In function `main':
test2.cc.text+0x68): undefined reference to `DerivedClass::setup()'
/tmp/ccaPvY8l.o: In function `DerivedClass:erivedClass[in-charge]()':
test2.cc.gnu.linkonce.t._ZN12DerivedClassC1Ev+0xd): undefined
reference to `BaseClass::BaseClass[not-in-charge]()'
collect2: ld returned 1 exit status
Can I change the declaration of DerivedClass so that I can use it as in
the code given? Or am I going about this the wrong way completely?
try to access functions of the derived class.
Simplified version of my code is as follows:
////////////////
// test2.hh
class BaseClass {
public:
BaseClass();
~BaseClass();
};
class DerivedClass : public BaseClass {
public:
DerivedClass() {}
~DerivedClass() {}
void setup();
};
////////////////
// test2.cc
#include "test2.hh"
int main() {
BaseClass* object;
object = new DerivedClass();
object->setup();
return 0;
}
When I try to compile this, I get the error:
test2.cc:8: error: `setup' undeclared (first use this function)
If I change the line to say:
object->DerivedClass::setup();
I get the error:
test2.cc:8: error: type `DerivedClass' is not a base type for type
`BaseClass'
And if I change the pointer declaration to say:
DerivedClass* object;
I get the linker error:
/tmp/ccaPvY8l.o: In function `main':
test2.cc.text+0x68): undefined reference to `DerivedClass::setup()'
/tmp/ccaPvY8l.o: In function `DerivedClass:erivedClass[in-charge]()':
test2.cc.gnu.linkonce.t._ZN12DerivedClassC1Ev+0xd): undefined
reference to `BaseClass::BaseClass[not-in-charge]()'
collect2: ld returned 1 exit status
Can I change the declaration of DerivedClass so that I can use it as in
the code given? Or am I going about this the wrong way completely?