DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Thread: state dp

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Posts
    1

    Question state dp

    hello:

    i am trying to reimplement the HF state DP in a new example c++ 2008:
    I am almost done, but it seem that polymorphism somehow is not working!
    this is the code:

    this FSM.h
    include <iostream>
    #include <string.h>

    class VendingMachine;

    class State {
    public:
    virtual ~State() = 0 {}
    virtual std::string nickel() const = 0;
    virtual std::string dime() const = 0;
    virtual std::string quarter() const = 0;
    };

    class startState : public State {
    private:
    VendingMachine *vendingmachine;
    startState( const startState& ); // Disable copy constructor
    void operator=( const startState& ); // Disable assignment operator

    public:
    startState(VendingMachine *vendingmachine);
    std::string nickel();
    std::string dime();
    std::string quarter();
    std::string toString();
    };

    class fiveState : public State {

    private:
    VendingMachine *vendingmachine;
    fiveState( const fiveState& ); // Disable copy constructor
    void operator=( const fiveState& ); // Disable assignment operator

    public:
    fiveState(VendingMachine *vendingmachine);
    std::string nickel();
    std::string dime();
    std::string quarter();
    std::string toString();
    };

    class tenState : public State {

    private:
    VendingMachine *vendingmachine;
    tenState( const tenState& ); // Disable copy constructor
    void operator=( const tenState& ); // Disable assignment operator

    public:
    tenState(VendingMachine *vendingmachine);
    std::string nickel();
    std::string dime();
    std::string quarter();
    std::string toString();
    };

    class twentyState : public State {
    private:
    VendingMachine *vendingmachine;
    twentyState( const twentyState& ); // Disable copy constructor
    void operator=( const twentyState& ); // Disable assignment operator

    public:
    twentyState(VendingMachine *vendingmachine);
    std::string nickel();
    std::string dime();
    std::string quarter();
    std::string toString();
    };

    class VendingMachine {
    private:
    State *startstate;
    State *fivestate;
    State *tenstate;
    State *fifteenstate;
    State *twentystate;
    State * _state;

    public:
    VendingMachine();
    void setState(State *state) ;
    std::string nickel();
    std::string dime();
    std::string quarter();
    State* getState();
    State* getstartState();
    State* getfiveState();
    State* gettenState();
    State* getfifteenState();
    State* gettwentyState();
    void tito();
    };

    //---------------------------------------------
    std::string VendingMachine::nickel() {
    return _state->nickel();
    }

    //---------------------------------------------
    std::string VendingMachine::dime() {
    return _state->dime();
    }

    //---------------------------------------------
    std::string VendingMachine::quarter() {
    return _state->quarter();
    }

    //---------------------------------------------
    void VendingMachine::setState(State *state) {
    _state = state;
    }

    //---------------------------------------------
    State* VendingMachine::getState() {
    return _state;
    }

    //---------------------------------------------
    State* VendingMachine::getstartState() {
    return startstate;
    }

    //---------------------------------------------
    State* VendingMachine::getfiveState() {
    return fivestate;
    }

    //---------------------------------------------
    State* VendingMachine::gettenState() {
    return tenstate;
    }

    //---------------------------------------------
    State* VendingMachine::getfifteenState() {
    return fifteenstate;
    }

    //---------------------------------------------
    State* VendingMachine::gettwentyState() {
    return twentystate;
    }

    //---------------------------------------------
    VendingMachine::VendingMachine( ) {

    startstate = new startState( this ); <------------here is the problem (comment this out and the abstract instantiation error disapears! obviously i need this to get polymorphic functionality)
    //fivestate = new fiveState(this);
    //tenstate = new tenState(this);
    //fifteenstate = new fifteenState(this);
    //twentystate = new twentyState(this);

    //_state = startstate;
    }


    //---------------------------------------------
    startState::startState(VendingMachine *vendingmachine) : vendingmachine( vendingmachine ) {
    this->vendingmachine = vendingmachine;
    }

    //---------------------------------------------
    std::string startState::nickel(){
    vendingmachine->setState(vendingmachine->getfiveState());

    return "";
    }

    //---------------------------------------------
    std::string startState::dime(){
    vendingmachine->setState(vendingmachine->gettenState());

    return "";
    }

    //---------------------------------------------
    std::string startState::quarter(){
    vendingmachine->setState(vendingmachine->getstartState());

    return "dispense";
    }

    //---------------------------------------------
    std::string startState::toString() {
    return "estado start";
    }

    //---------------------------------------------
    fiveState::fiveState(VendingMachine *vendingmachine) : vendingmachine( vendingmachine ) {
    this->vendingmachine = vendingmachine;
    }

    //---------------------------------------------
    std::string fiveState::nickel() {
    vendingmachine->setState(vendingmachine->gettenState());

    return "";
    }

    //---------------------------------------------
    std::string fiveState::dime() {
    vendingmachine->setState(vendingmachine->getfifteenState());

    return "";
    }

    //---------------------------------------------
    std::string fiveState::quarter() {
    vendingmachine->setState(vendingmachine->getstartState());

    return "dispense";
    }

    //---------------------------------------------
    std::string fiveState::toString() {
    return "estado five";
    }

    //---------------------------------------------
    tenState::tenState(VendingMachine *vendingmachine) : vendingmachine( vendingmachine ) {
    this->vendingmachine = vendingmachine;
    }

    //---------------------------------------------
    std::string tenState::nickel() {
    vendingmachine->setState(vendingmachine->getfifteenState());

    return "";
    }

    //---------------------------------------------
    std::string tenState::dime() {
    vendingmachine->setState(vendingmachine->gettwentyState());

    return "";
    }

    //---------------------------------------------
    std::string tenState::quarter() {
    vendingmachine->setState(vendingmachine->getstartState());

    return "dispense";
    }

    //---------------------------------------------
    std::string tenState::toString() {
    return "estado ten";
    }

    //---------------------------------------------
    twentyState::twentyState(VendingMachine *vendingmachine) : vendingmachine( vendingmachine ) {
    this->vendingmachine = vendingmachine;
    }

    //---------------------------------------------
    std::string twentyState::nickel() {
    vendingmachine->setState(vendingmachine->getstartState());

    return "dispense";
    }

    //---------------------------------------------
    std::string twentyState::dime() {
    vendingmachine->setState(vendingmachine->getstartState());

    return "dispense";
    }

    //---------------------------------------------
    std::string twentyState::quarter() {
    vendingmachine->setState(vendingmachine->getstartState());

    return "dispense";
    }

    //---------------------------------------------
    std::string twentyState::toString() {
    return "estado twenty";
    }


    this is FSM.cpp
    #include "stdafx.h"
    #include "FSM.h"


    //int _tmain(int argc, _TCHAR* argv[])
    int main()
    {
    VendingMachine* vendingmachine = new VendingMachine();
    std::cout << vendingmachine->getState();
    return 0;
    }

    I am getting error 2259 cannot instantiate abstract class.

    Please help, how can i overcome this to have the code working?

    Thank you very much.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    There are several problems with your code.
    First, you can't pass this as an argument to a constructor because at that point, there is no valid this. You can refer to the this pointer only from within the context of an existing object (that includes a constructor body, but not its parameter list).
    You also have two accidental cases of hiding instead of overriding:
    [C++ Warning] functors.cpp(72): W8022 'twentyState::quarter()' hides virtual function 'State::quarter() const'.
    [C++ Warning] functors.cpp(72): W8022 'twentyState::dime()' hides virtual function 'State::dime() const'.
    I suppose you forgot to add const at the end of the latter.
    Danny Kalev

Similar Threads

  1. Finite State Machines
    By Hunterlmc in forum .NET
    Replies: 3
    Last Post: 04-14-2008, 07:09 AM
  2. Java applet to VB
    By Maurice in forum Java
    Replies: 0
    Last Post: 02-11-2003, 01:10 AM
  3. Web Farm Application State
    By Al Guten in forum ASP.NET
    Replies: 0
    Last Post: 11-14-2002, 12:42 PM
  4. ASP+ session state management
    By Ariel Azia in forum ASP.NET
    Replies: 3
    Last Post: 08-02-2000, 01:10 AM
  5. Programatic Listview item select problems..
    By Matt Williamson in forum VB Classic
    Replies: 4
    Last Post: 04-17-2000, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links