Ciao a tutti.
cerco di creare una lista di classi utilizzando vector dell'STL. Con il primo metodo fun<ziona, con il secondo va in crash non trovando la funzione virtuale nella tabella. Compilatore utilizzato gcc.
Chi sa dirmi il perchè?
includo tutti i file:
codice:
//// Main
#include <iostream>
#include "TOcc.h"
#include "TColl.h"
#include <vector>
using namespace std;
int main()
{
DBE dbe;
cout << endl << " Metodo 1. Funziona" << endl;
vector<_TOccV<DBE> *> list;
_TOccV<DBE> *p;
TOcc1 s1;
p = &s1;
list.push_back(p);
TOcc2 s2;
p = &s2;
list.push_back(p);
list[0]->Build ();
list[1]->Build ();
cout << endl << " Metodo 2. Non funziona" << endl;
TColl list2;
list2.BuildAll ();
return 0;
}
codice:
///// TOcc.h
#ifndef TOCC_H_INCLUDED
#define TOCC_H_INCLUDED
#include<assert.h>
#include<vector>
using namespace std;
class DBE {
vector<int> someData;
public:
DBE () {};
};
template <typename S>
class _TOccV {
vector< vector<int> > *_data;
int _dataYSize;
virtual void _Build (void) { cout << endl << "_TOccV::_Build" << endl; };
public:
_TOccV () : _dataYSize(0) { };
_TOccV (int dataYSize) : _dataYSize(dataYSize)
{ _data = new vector< vector<int> >(dataYSize); };
~_TOccV ()
{ delete _data; _data = NULL; };
void Push (vector<int> *v, int where) { _data[where].push_back(*v); };
vector<int> Pop (int what) { assert(what < _data->size()); return (*_data)[what]; };
void Build (void) { _Build (); };
};
class TOcc1 : public _TOccV<DBE> {
void _Build (void) { cout << endl << "TOcc1::_Build" << endl; };
public:
TOcc1 () : _TOccV<DBE> (1) {};
};
class TOcc2 : public _TOccV<DBE> {
void _Build (void) { cout << endl << "TOcc2::_Build" << endl; };
public:
TOcc2 () : _TOccV<DBE> (1) {};
};
#endif // TOCC_H_INCLUDED
codice:
/////// TColl.h
#ifndef TCOLL_H_INCLUDED
#define TCOLL_H_INCLUDED
#include <vector>
#include "TOcc.h"
using namespace std;
class TColl {
vector<_TOccV<DBE> *> _list;
public :
TColl ();
void BuildAll (void);
};
TColl::TColl ()
{
_TOccV<DBE> *p;
TOcc1 s1;
p = &s1;
_list.push_back(p);
TOcc2 s2;
p = &s2;
_list.push_back(p);
};
void TColl::BuildAll (void)
{
unsigned int idx;
_TOccV<DBE> *p;
for (idx = 0; idx < _list.size(); idx++)
{
p = _list[idx];
p->Build ();
}
}
#endif // TCOLL_H_INCLUDED