See an article on your Web site: "Insert different types of objects in an STL list (Lists)." I feel that the answer has not pointed out the nature of the problem, so I put forward my point of view, I beg to correct. I think that the cause of the error is the pointer transformation process, the program did not point out the original prototype of the pointer, or, because did not find the correct subclass function address to invoke the error, my original code is as follows: With Dev-cpp g++ compiled.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
/**
* Parent class: Synobject
*/
Class Synobject {
Public:
Synobject ();
String GetClass ();
String ClassName;
};
Synobject::synobject ()
{
ClassName = "Synobject";
}
String Synobject::getclass ()
{
return className;
}
/**
* Sub-class 1:synpin
*/
Class Synpin:public Synobject {
string pin;
Public:
Synpin ();
void Setpin (string Pin);
String Getpin ();
Private
};
Synpin::synpin ()
{
ClassName = "Synpin";
}
void Synpin::setpin (String Pin)
{
pin = pin;
}
String Synpin::getpin ()
{
return pin;
}
/**
* Sub-class 2:syncell
*/
Class Syncell:public Synobject {
string cell;
Public:
Syncell ();
void Setcell (string Cell);
String Getcell ();
Private
};
Syncell::syncell ()
{
ClassName = "Syncell";
}
void Syncell::setcell (String Cell)
{
cell = cell;
}
String Syncell::getcell ()
{
return cell;
}
/**
* The system runs the main program
*/
int main ()
{
file://Build Objects
Synobject * PMYOBJECT;
Pmyobject = new Synobject;
Synpin * PMYPIN;
Pmypin = new Synpin;
Pmypin->setpin ("Mypin");
Syncell * Pmycell;
Pmycell = new Syncell;
Pmycell->setcell ("MyCell");
Insert Object
Vector<synobject *> Myvector;
Myvector.empty ();
Myvector.push_back (Pmyobject);
Myvector.push_back (Pmypin);
Myvector.push_back (Pmycell);
Calling Object
Vector<synobject *>::iterator Thisvector=myvector.begin ();
cout<< "program begin:" <<endl;
while (Thisvector!= myvector.end ())
{
cout << (**thisvector). GetClass () << Endl;
if ((**thisvector). GetClass (). Compare ("Syncell") = = 0)
{
cout << ((syncell**) thisvector)). Getcell () << Endl;
}
if ((**thisvector). GetClass (). Compare ("synpin") = = 0)
{
cout << (* * (synpin**) thisvector). Getpin () << Endl;
}
thisvector++;
}
}
End of program