473,378 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

Neo
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;

}

Mar 28 '06 #1
1 10869
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
Mar 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
17
by: DanielESFA | last post by:
Hey guys :) This is a bit of a funny one... We're four guys working on the same project, everybody using KDevelop and g++ on Linux. Three of us are using Mandrake, with g++ 3.4.3 and 3.4.1....
5
by: Mars | last post by:
#include <stdio.h> #include <string.h> int main(void) { char* name="Smith SYN is a man."; char* sure="SyN"; char* s; int x,y;
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
5
by: prashanth | last post by:
Hi All, I have few link errors that i am not able to get rif off in a VC++ project (plugin for filemaker) where i am trying to port the code written for a mac to windows. I am creating a...
9
by: July | last post by:
Hello! consider the following code: class A { public: virtual void f() const{ cout << "A::f()" << endl; } };
2
by: John | last post by:
Hello! When I compile the following code, I get this error message "error: passing ... discards qualifiers" and I don't understand why. Could anybody help me? Thank you! John
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
4
by: silverburgh.meryl | last post by:
I have code which uses Boost lambda in a template like this: using namespace boost::lambda; template<class T> bool lessThanXY( T& src, T& dest ) { return (src.getY() < dest.getY()); } ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.