Error: passing `const state' as `this' argument of `void state::dumpState()' discards qualifiers

Discussion in 'C++' started by Neo, Mar 28, 2006.

  1. Neo

    Neo Guest

    hi,

    This is my first post in C++ group, so please be nice to me. Thanks.
    Also, I will post my first C++ program following. There is a compile
    error, I cannot figure it out even I can fix it. And please give me
    your comments on my program as much as you can. I think I need some
    basic programming convention of C++.

    BTW, I used C a lot before, so if you can explain me from low-level
    view that would be great!

    /* ------------------------------------------------------------
    * state.hpp
    * ----------------------------------------------------------*/
    #ifndef _STATE_HPP

    #define _STATE_HPP

    #include <map>
    #include <string>
    #include <iostream>


    using namespace std;

    class state {

    private:
    map<string, state *> p_trans;
    bool b_accept;
    bool b_start;

    public:
    string p_state_ID;

    state(string p_state_id, bool start, bool accept);
    state::state();
    ~state();
    void addTrans(string symbol, state * target);
    state * delta(string symbol);
    bool operator<(state s) const
    { return (p_state_ID<s.getStateID()); }

    string getStateID();

    void dumpState();

    };

    #endif /* _STATE_HPP */

    /* ------------------------------------------------------
    * state.cpp
    * ---------------------------------------------------- */
    #include <iostream>
    #include <string.h>

    #include "state.hpp"

    state::state(string p_state_id, bool start, bool accept) {

    p_state_ID = p_state_id;

    b_start = start;
    b_accept = accept;

    // p_trans = new map<string, state>();
    }

    /*

    state::state(const state& r_state) {

    p_state_ID = r_state.p_state_ID;

    b_start = r_state.b_start;

    b_accept = r_state.b_accept;

    }

    */

    state::~state() {

    }

    void state::addTrans(string symbol, state *target) {
    p_trans[symbol] = target;
    }

    state * state::delta(string symbol) {
    return p_trans[symbol];
    }

    string state::getStateID() {
    return p_state_ID;
    }
    void state::dumpState() {

    cout<<"state name =\""<<p_state_ID<<"\".";
    cout<<"accept = \""<<b_accept<<"\".";
    cout<<"start = \""<<b_accept<<"\".\n";

    map<string, state*>::iterator p;

    for (p = p_trans.begin(); p!= p_trans.end(); ++p) {
    cout<<p->first<<'\t'<<p->second->getStateID()<<'\n';
    }
    }
    /* -----------------------------------------------
    * automata.cpp
    * ---------------------------------------------*/
    #include "automata.hpp"

    automata::automata(string v_name) {
    name = v_name;
    trap_st = addState("trap", false, false);
    start_st = NULL;
    trap_st = NULL;
    }

    automata::~automata() {
    }

    void automata::addTransition(state s, string a, state t) {
    s.addTrans(a, &t);
    }

    state * automata::addState(string state_id, bool b_start, bool
    b_accept) {

    state * st = new state(state_id, b_start, b_accept);

    states_set.insert(*st);

    if (b_start == true) {
    start_st = st;
    }

    if (b_accept == true) {
    accept_states_set.insert(*st);
    }
    return st;
    }

    string automata::addSymbol(string id) {
    symbols_set.insert(id);
    return id;
    }

    state * automata::delta(state * p_str_st, string a) {
    state *target = p_str_st->delta(a);
    return (target == NULL) ? trap_st : target;
    }
    state* automata::Delta(state * p_str_st, vector<string> vec_sigma) {
    state * tmp = p_str_st;
    for (unsigned int i = 0; i < vec_sigma.size(); i++) {
    tmp = delta(tmp, vec_sigma.at(i));
    }
    return tmp;
    }

    void automata::dumpAutomata() {
    set<state, less<state> >::const_iterator it_st;
    for (it_st = states_set.begin(); it_st != states_set.end();
    it_st++) {
    // state st = (state)(*it_st);
    // ((state)(*it_st)).dumpState(); <-- It works. but why?
    it_st->dumpState(); // <-- It cannot work. Why?
    }
    }
    /* ------------------------------------------------------------
    * automata.hpp
    * --------------------------------------------------------- */
    #ifndef _AUTOMATA_HPP

    #define _AUTOMATA_HPP

    #include "state.hpp"
    #include <set>
    #include <vector>
    #include <string>
    #include <iostream>

    class automata {

    private:
    /* The name of this automata */
    string name;

    set<string> symbols_set;
    /* All states of this automata */
    set<state, less<state> > states_set;
    /* All accepted stats of this automata */
    set<state, less<state> > accept_states_set;

    state *start_st;
    state *trap_st;

    state * delta(state * p_str_st, string a);

    public:

    automata(string v_name);

    ~automata();

    state* addState(string state_id, bool b_start, bool b_accept);

    void addTransition(state p_dst_st, string str);

    state* Delta(state * p_str_st, vector<string> vec_sigma);

    set<state, less<state> > DeltaLifted(set<state, less<state> >
    src_set, vector<string> vec_sigma);

    void addTransition(state s, string a, state t);

    string addSymbol(string id);

    void dumpAutomata();

    };
    /* -------------------------------------------------------
    * test_automata.cpp
    * --------------------------------------------------------
    #include "automata.hpp"
    #include <iostream>


    int main(int argc, char ** argv) {

    cout<<"----->Begin Test Automata\n";

    cout<<"<-----End Test Automata\n";

    automata * ocpa = new automata("OpenClose");

    state * p_closed = ocpa->addState("closed", true, true);
    state * p_opened = ocpa->addState("opened", false, false);

    ocpa->dumpAutomata();

    string open = ocpa->addSymbol("open");
    string close = ocpa->addSymbol("close");

    ocpa->addTransition(*p_closed, open, *p_opened);
    ocpa->addTransition(*p_opened, close, *p_closed);

    return 0;

    }
     
    Neo, Mar 28, 2006
    #1
    1. Advertising

  2. Neo wrote:
    > This is my first post in C++ group, so please be nice to me. Thanks.


    We play nice with those who play nice with us.

    > Also, I will post my first C++ program following. There is a compile
    > error, I cannot figure it out even I can fix it. And please give me
    > your comments on my program as much as you can. I think I need some
    > basic programming convention of C++.
    >
    > BTW, I used C a lot before, so if you can explain me from low-level
    > view that would be great!
    >
    > /* ------------------------------------------------------------
    > * state.hpp
    > * ----------------------------------------------------------*/
    > [...]
    > void automata::dumpAutomata() {
    > set<state, less<state> >::const_iterator it_st;
    > for (it_st = states_set.begin(); it_st != states_set.end();
    > it_st++) {
    > // state st = (state)(*it_st);
    > // ((state)(*it_st)).dumpState(); <-- It works. but why?


    Because you use a C cast which removes the 'const' qualifier. Don't do
    that, it's a BAD IDEA(tm).

    > it_st->dumpState(); // <-- It cannot work. Why?


    The operator-> of 'const_iterator' returns a reference to const 'state'.
    'dumpState' is a non-const function. You cannot call non-const member
    function for a const object.

    To solve either declare 'dumpState' const, or use a regular iterator
    here (".. ::iterator it_st;")

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 28, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. danny van elsen
    Replies:
    6
    Views:
    41,237
    Pete Becker
    Apr 24, 2005
  2. Jason
    Replies:
    6
    Views:
    9,008
    Mike Wahler
    Oct 9, 2003
  3. Charlie Zender
    Replies:
    12
    Views:
    1,086
    Peter Pichler
    Jan 3, 2004
  4. Lycan. Mao..
    Replies:
    4
    Views:
    3,315
    Lycan. Mao..
    Mar 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,123
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page