Hello all,
I have a simple class hierarchy, with a few different subclasses inheriting from one superclass. Objects of these subclasses are all put in a list together, and i want to iterate through the list and call a member function of the instances which is common to all the subclasses. I keep getting an 'unresolved external' error when I try to call the function. I'll post some minimal code below to try to illustrate my problem.
// declarations
class superclass
{
public:
virtual void printDetails();
};
class subclass: public superclass
{
public:
void printDetails();
};
// definitions
class superclass
{
public:
virtual void printDetails();
};
class subclass: public superclass
{
public:
void printDetails(){// prints the details of the subclass instance};
};
// main function
#include "ClassDefinitions.h"
#include <list>
list<superclass> stock;
subclass1 inst1 = subclass1();
stock.insert(stock.end(), inst1);
list<item>::iterator i;
for (i = stock.begin(); i != stock.end(); ++i) { i->printDetails(); }
// resulting error
unresolved external symbol "public: virtual void __thiscall item::printDetails(void)" (?printDetails@item@@UAEXXZ) referenced in function _main
I think my problem is that the list stores objects of type superclass, so when i try to call 'printDetails()' it tries to call the member function of superclass, not subclass. I tried to fix this problem by making printDetails a virtual function, but I dont think I understand this polymorphism stuff well enough. Do I need to do some typecasting? Should I have made it a list of a different type?
Many thanks in advance for the help, sorry if I've missed something obvious.
You probably forgot to define printDetails for one of the classes.
Your other problem is that you're trying to store the objects directly in the container. That won't work, as you'll only store a copy of the superclass portion - this is called object slicing. Store pointers instead.
It looks like you are definiting the classes twice.
This is the class definition
1 2 3 4 5
class superclass
{
public:
virtualvoid printDetails();
};
This is the definition of the function superclass::printDetails()
1 2 3 4
void superclass::printDetails()
{
// put some code here
}
When you declare a list as list<superclass> stock; that list can only store objects of type superclass. It can't even store objects of subclass. What you have to do is use pointers. A pointer superclass* can point to any object of type superclass or any of it's subclasses.
Hi, thanks for the quick reply.
I've double checked that printDetails is declared and defined for each subclass.
I see now that because I used the superclass to define the type of the list (in list<superclass> stock;), when i add the subclass instances to the list it typecasts them into the superclass, so it cant use the printDetails member functions defined only in the subclasses. Is there a way I can avoid this typecasting?
I'm changing it to be a list of pointers instead, but I'm having trouble there too.
I've changed the list declaration to
list<item*> stock;
and the iterator declaration to
list<item*>::iterator i;
but i cant seem to find a way to get to the member functions using the iterator.