Domanda

I must to do a list of template abstract base classes (and I have the delivered classes too) but I don't can inizialize the element of my list because the element is an abstract class...

this is my declaration:

/* fsm (list node) declaration */
template<class step_type> class fsm {
    protected:
        step_type   step;
        step_type   step_old;
        step_type   step_tmp;
        char        name[256];
        fsm *next;
        fsm *prev;
    public:
        fsm(step_type step);
        virtual void update() = 0;
        void show(){cout << step << ' ' << step_tmp << '\n'; };
        void init(step_type st_current) {step = st_current;};
//metodi per gestione nodo lista
        step_type getStep()     { return step; }
        fsm* getNext()          { return next; }
        fsm* getPrev()          { return prev; }

        void setStep(step_type s)   { step = s; }
        void setNext(fsm *n)        { next = n; }
        void setPrev(fsm *p)        { prev = p; }

};

/* fsm_List declaration */
template <class step_type>
class fsm_List
{
    fsm<step_type> *head, *tail;
    int size;

public:
    fsm_List();

    fsm<step_type>* getHead()    { return head; }
    fsm<step_type>* getTail()    { return tail; }
    int getSize()        { return size; }

    void insert(fsm<step_type> *n);    // add node to list
    void insert(step_type &value);        // new node and add in list
    fsm<step_type> *search(step_type &value);    //first node with value
    void delnode(fsm<step_type> *n);    // remove node
    int delvalue(step_type &value);        // remove all nodes

};

this is my delivered class:

class deri_pinza : public fsm<pin_steps>{
    private:
        bool    cmd_prelevamento_done;
    public:
        deri_pinza(): fsm<pin_steps>(ST_PIN_BOOT){
            cmd_prelevamento_done = false;
        };
        void update();
};

where:

enum pin_steps {
    ST_PIN_BOOT,
    ST_PIN_CHECK_MOTORE,
    ST_PIN_ZERO_MOTORE,
    ST_PIN_WAIT_ZERO_MOTORE,
    ST_PIN_OPEN,
    ST_PIN_READY,
};

I have tryed to test in my main, but it's wrong...

    fsm<pin_steps> *one, *two, *three, *four, *five;

    one = new fsm<pin_steps>(ST_PIN_CHECK_MOTORE);
    two = new fsm<pin_steps>(ST_PIN_ZERO_MOTORE);
    three = new fsm<pin_steps>(ST_PIN_WAIT_ZERO_MOTORE);
    four = new fsm<pin_steps>(ST_PIN_OPEN);
    five = new fsm<pin_steps>(ST_PIN_READY);


    fsm_List<pin_steps> *mylist = new fsm_List<pin_steps>();

    (*mylist)+=(*one);
    (*mylist)+=(*two);


    mylist->insert(one);
    mylist->insert(two);


    cout << *mylist << endl;

how can I inizialize the List without inizialize fsm ( abstract class)?

È stato utile?

Soluzione

you can't create an instance of fsm<> with new, since it's abstract - it contains the pure virtual method virtual void update()=0;

you can for example:

fsm<pin_steps> *one...
one = new deri_pinza;

this is legal - and go on from here...

EDIT - followup to our comments:

if you need a more general deri pinza (a generic one), it can be defined as:

template <typename STEP_TYPE>
class deri_pinza_gen : public fsm<STEP_TYPE> {
private:
    bool cmd_prelevamento_done;
public:
    deri_pinza_gen(STEP_TYPE step) : fsm<STEP_TYPE>(step){
        cmd_prelevamento_done = false;
    };
    virtual void update();
    virtual ~deri_pinza_gen();
};

and then:

mylist->insert( new deri_pinza_gen<pin_steps>(ST_PIN_BOOT) );
mylist->insert( new deri_pinza_gen<pin_steps>(ST_PIN_CHECK_MOTORE) );

ANOTHER_list->insert( new deri_pinza_gen<ANOTHER_pin_steps>(ANTHER_enum_item) );
...

are valid insertions. I have declared update() virtual here, so you can derive your deri_pinza from this one, if you need it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top