Interpreter Errors !!!!

In my interpreter member functions are not declared in a class.
I tried declaring them in a class but I do not know which data type to use.
I've tried using void, int, class, struct, friend but it leads to more error.

This is how the member functions are used in implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
list::const_iterator i = s.idList.begin();
    for ( ; i != s.idList.end(); i++)
    out << *i;
    out << endl;
    return out;
list::iterator i = find(idList.begin(),idList.end(),tmp);
    if (i != idList.end())
    return i->value;
    else issueError("Unknown variable");
list::iterator i = find(idList.begin(),idList.end(),tmp);
    if (i != idList.end())
    i->value = e;
    else idList.push_front();


idList is a member of the class Statement is type IdNode from my Idnode class.Here is the class statement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Statement {
public:
    Statement() { 
    }
    void getStatement();
private:
    IdNode idList;
    char  ch;
    double factor();
    double term();
    double expression();
    void readId(char*);
    void issueError(char *s) { 
        cerr << s << endl; exit(1); 
    }
    double findValue(char*);
    void  processNode(char*, double);
    friend ostream& operator<< (ostream& out, Statement& s) {
        list::const_iterator i = s.idList.begin();
        for ( ; i != s.idList.end(); i++)
        out << *i;
        out << endl;
        return out;
    }
};


The member functions used was .end(), .begin(), and .push_front()
They all have the same error saying that they are not members of the class IdNode.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class IdNode {
public:
    IdNode(char *s = "", double e = 0) {
        id = strdup(s);
        value = e;
    }
    bool operator== (const IdNode& node) const {
        return strcmp(id,node.id) == 0;
    }
    IdNode *prev;
    IdNode *next;
    string element;

  
private:   
    char *id;
    double value;
    friend class Statement;
    friend ostream& operator<< (ostream& out, const IdNode& r) {
        out << r.id << " = " << r.value << endl;
        return out;
    }
};


However, these three member functions in my other class list.
1
2
3
4
5
6
7
8
9
10
11
12
class list
{
      IdNode *head, *tail;
      public:
             typedef Iterator iterator;
             typedef Iterator const_iterator;
             Iterator begin (const string&);
             Iterator end (const string&);
             Iterator push_front ();
             bool empty() const { return head==NULL; }
             
};


I cannot declare the functions as list begin(), etc in my IdNode class since my list class depends on my Iterator class which in turn depends on my IdNode class. Here is the Iterator class.
1
2
3
4
5
6
7
8
9
10
class Iterator
{
      public:
      IdNode *node;
      Iterator &operator ++ (int);
      IdNode &operator * ();
      IdNode *operator -> ();
      bool operator != (bool);

};


Is there any way to fix declare these functions in my class IdNode or implement the codes in a similar way so that they would use the functions declared in my class list. This is the only error in my program

HELP PLS.... =(
only have a few more days to go before submission of this project.
Thanks to those who will help =)


Last edited on
1
2
3
4
5
6
7
8
class Statement{
  IdNode idList; //maybe this should be list idList
}
s.idList.begin(); //you're asking for the function in IdNode

class list{
  Iterator begin (const string&); //there isn't begin(), it must receive a string
}
Last edited on
I have already tried chainging the IdNode Idlist to just list Idlist
However, other errors such as
1
2
 In member function `void Statement::processNode(char*, double)':  
no matching function to call `list::begin()'

this is because the member function Iterator begin(); in the class function was changed into Iterator begin(const string&); to solve the error when implementing Iterator list :: begin() and other member functions in the list class.

I solved the errors by replacing list::begin() to list::begin(const string&) as it is a candidate solution provided the compiler.

New error messages arises from this solution. The new error message is now
expected primary-expression before "const"
Now i do not know what 'primary-expression' we should put befor 'const'.
In my interpreter member functions are not declared in a class.
I tried declaring them in a class but I do not know which data type to use.
I've tried using void, int, class, struct, friend but it leads to more error.

Later you declare (and define) methods on class Statement. I don't understand what are you saying. And class, struct, friend are not data type.

1
2
3
4
5
class list{
  Iterator begin (const string&); //why are you asking for a string?
  Iterator end (const string&);
  Iterator push_front ();           //push will add an element. You are not asking for one
}


I have already tried chainging the IdNode Idlist to just list Idlist
So what is it, a node or a list?

no matching function to call `list::begin()'
Again, why is begin asking for a string?
You say that you changed it because you were having errors with Iterator list::begin(). What errors?

I solved the errors by replacing list::begin() to list::begin(const string&) as it is a candidate solution provided the compiler.
where are you doing the replacement? could you show the update code?


Here is the whole and updated code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//**************************  interpreter.h   ***********************

#ifndef INTERPRETER
#define INTERPRETER

#include <iostream>

//#include <list>
//#include <algorithm> // find()

using namespace std;

class IdNode {
public:
    IdNode(char *s = "", double e = 0) {
        id = strdup(s);
        value = e;
    }
    bool operator== (const IdNode& node) const {
        return strcmp(id,node.id) == 0;
    }
    IdNode *prev;
    IdNode *next;
    string element;

  
private:   
    char *id;
    double value;
    friend class Statement;
    friend ostream& operator<< (ostream& out, const IdNode& r) {
        out << r.id << " = " << r.value << endl;
        return out;
    }
};

class Iterator
{
      public:
      IdNode *node;
      Iterator &operator ++ (int);
      IdNode &operator * ();
      IdNode *operator -> ();
      bool operator != (bool);

};

class list
{
      IdNode *head, *tail;
      public:
             typedef Iterator iterator;
             typedef Iterator const_iterator;
             Iterator begin (const string&);
             Iterator end (const string&);
             Iterator push_front ();
             bool empty() const { return head==NULL; }
             
};



class Statement {
public:
    Statement() { 
    }
    void getStatement();
private:
    list idList;
    char  ch;
    double factor();
    double term();
    double expression();
    void readId(char*);
    void issueError(char *s) { 
        cerr << s << endl; exit(1); 
    }
    double findValue(char*);
    void  processNode(char*, double);
    friend ostream& operator<< (ostream& out, Statement& s) {
        list::const_iterator i = s.idList.begin(const string&);
        for ( ; i != s.idList.end(const string&); i++)
        out << *i;
        out << endl;
        return out;
    }
};

#endif
//**************************  interpreter.cpp   ***********************

#include <cctype>
#include "interpreter.h"

Iterator list :: begin(const string& h)
{
              if(empty())
    {
       IdNode* temp = new IdNode;
       head = temp;
       tail = temp;
       temp->prev = NULL;
       temp->next = NULL;
       temp->element = h;
    }
    else
    {
       IdNode* curr;
       curr = tail;
       while( h>curr->element && curr->prev != head->prev) curr = curr->prev;

       if(curr == tail)
       {
         IdNode* temp = new IdNode;
         temp->element = h;
         temp->next = curr;
         temp->prev = NULL;
         head->prev = temp;
         head = temp;
     
       }
       else
       {
       if(curr == head && h>head->element)
       {
         head->prev = new IdNode;
         (head->prev)->next = head;
         head = head->prev;
         head->prev = NULL;
         head->element = h;
      
       }
       else
       {
         IdNode* temp = new IdNode;
         temp->element = h;
         temp->prev = curr;
         (curr->next)->prev = temp;
         temp->next = curr->next;
         curr->next = temp;
      
       }
      }
    }
}

Iterator list :: end(const string& t)
{
         if(empty())
    {
       IdNode* temp = new IdNode;
       head = temp;
       tail = temp;
       temp->prev = NULL;
       temp->next = NULL;
       temp->element = t;
    }
    else
    {
       IdNode* curr;
       curr = head;
       while( t>curr->element && curr->next != tail->next) curr = curr->next;

       if(curr == head)
       {
         IdNode* temp = new IdNode;
         temp->element = t;
         temp->prev = curr;
         temp->next = NULL;
         head->next = temp;
         tail = temp;
     
       }
       else
       {
       if(curr == tail && t>tail->element)
       {
         tail->next = new IdNode;
         (tail->next)->prev = tail;
         tail = tail->next;
         tail->next = NULL;
         tail->element = t;
     
       }
       else
       {
         IdNode* temp = new IdNode;
         temp->element = t;
         temp->next = curr;
         (curr->prev)->next = temp;
         temp->prev = curr->prev;
         curr->prev = temp;
      
       }
      }
    }
}

Iterator list :: push_front()
{
}

double Statement::findValue(char *id) {
    IdNode tmp(id);
   list::iterator i = find(idList.begin(const string&),idList.end(const string&),tmp);
    if (i != idList.end(const string&))
         return i->value;
    else issueError("Unknown variable");
    return 0;  // this statement will never be reached;
}

void Statement::processNode(char* id ,double e) {
    IdNode tmp(id,e);
    list::iterator i = find(idList.begin(const string&),idList.end(const string&),tmp);
    if (i != idList.end(const string&))
    i->value = e;
    else idList.push_front();
}

// readId() reads strings of letters and digits that start with
// a letter, and stores them in array passed to it as an actual
// parameter. 
// Examples of identifiers are: var1, x, pqr123xyz, aName, etc.

void Statement::readId(char *id) {
    int i = 0;
    if (isspace(ch))
         cin >> ch;       // skip blanks;
    if (isalpha(ch)) {
         while (isalnum(ch)) {
             id[i++] = ch;
             cin.get(ch); // don't skip blanks;
         }
         id[i] = '\0';
    }
    else issueError("Identifier expected");
}

double Statement::factor() {
    double var, minus = 1.0;
    static char id[200];
    cin >> ch;
    while (ch == '+' || ch == '-') {      // take all '+'s and '-'s.
        if (ch == '-')
            minus *= -1.0;
        cin >> ch;
    }
    if (isdigit(ch) || ch == '.') {      // Factor can be a number
         cin.putback(ch);
         cin >> var >> ch;
    }
    else if (ch == '(') {                  // or a parenthesized expression,
         var = expression();
         if (ch == ')')
              cin >> ch;
         else issueError("Right paren left out");
    }
    else {
         readId(id);                          // or an identifier.
         if (isspace(ch))
             cin >> ch;
         var = findValue(id);
    }
    return minus * var;
}

double Statement::term() {
    double f = factor();
    while (true) {
        switch (ch) {
            case '*' : f *= factor(); break;
            case '/' : f /= factor(); break;
            default  : return f;
        }
    }
}

double Statement::expression() {
    double t = term();
    while (true) {
        switch (ch) {
            case '+' : t += term(); break;
            case '-' : t -= term(); break;
            default  : return t;
        }
    }
}

void Statement::getStatement() {
    char id[20], command[20];
    double e;
    cout << "Enter a statement: ";
    cin  >> ch;
    readId(id);
    strcpy(command,id);
    for (int i = 0; i < strlen(command); i++)
	command[i] = toupper(command[i]);
    if (strcmp(command,"STATUS") == 0)
         cout << *this;
    else if (strcmp(command,"PRINT") == 0) {
         readId(id);
         cout << id << " = " << findValue(id) << endl;
    }
    else if (strcmp(command,"END") == 0)
         exit(0);
    else {
         if (isspace(ch))
             cin >> ch;
         if (ch == '=') {
              e = expression();
              if (ch != ';')
                   issueError("There are some extras in the statement");
              else processNode(id,e);
         }
         else issueError("'=' is missing");
    }
}
//**************************  useInterpreter.cpp   ***********************

#include "interpreter.h"


int main() {
    Statement statement;
    cout << "The program processes statements of the following format:\n"
         << "\t<id> = <expr>;\n\tprint <id>\n\tstatus\n\tend\n\n";
    while (true)                  // This infinite loop is broken by exit(1)
        statement.getStatement(); // in getStatement() or upon finding 
    return 0;                     // an error.
}


list::begin () is asking for a string since the implementation list::begin () in interpreter.h is Iterator list::begin (const string& h) the same goes for list::end () which is implemented as Iterator list::end (const string& t)

Therefore to solve for the problem
1
2
no matching function for call to `list::begin()'
candidates are: Iterator list::begin(const std::string&)  


i added (const string &) to every begin() and end(). Now the new error is
expected primary-expression before "const"
1
2
3
4
5
6
7
void foo(const std::string &s){
  std::cout << s << std::endl;
}

int main(){
  foo( "hello_world" ); //when you call the function, you pass an object
}
Another error is
1
2
bool Iterator::operator !=(bool); //it should be
bool Iterator::operator != (const Iterator &);//or at least you are using in this way 

(Also it seems that your class iterator doesn't work with the STL find)
Last edited on
Is there anyway to use the functions .begin and .end without passing an object since I want them to work the same way as it does when is use #include <list> .

Is my implementation for the functions begin, end and push_front correct since I could not find any way to iplement those functions in order to make them do the same things as those functions in #include <list> .

Is there any other way of solving the original problem when list IdList was just IdNode IdList Even if i have to change the way the member functions are implemented?

PLS HELP. this is due two days from now.
Why don't just overload that methods?
1
2
3
4
5
6
void foo(const string &s){
  std::cout << s << std::endl;
}
int foo(){
  return 42;
}
If you can explain me what does the begin(const string &) maybe I could help you. (it seems more a find and insert, so how do you find nothing?)

Is there any other way of solving the original problem when list IdList was just IdNode IdList Even if i have to change the way the member functions are implemented?
What kind of object is IdList? if you make it IdNode, and implement IdNode as IdList, then what is the point?
begin (const string&) returns iterator to the beginning. Thus when it is implementer in Iterator list :: begin (const string& h) it is implemented as a linked list in order to return iterator to the beginning or the head of the list. it is the same for end (const string&).

begin should have (const string& h) so that the h in the implementation of Iterator list :: begin will be declared.

PS. This post is an extention of my original post which is also about the interpreter. Here is an earlier post about the same topic
http://www.cplusplus.com/forum/general/29109/
think of an iterator as a pointer (it has a pointer to a node)
list::begin() should be pointing to the first member in your list, or list::begin() == list::end() if the list is empty.
list::end() is an invalid position (you can't dereference it).
1
2
3
4
5
6
//I use one that points one cell back that the actual node
iterator list::begin(){
  iterator temp;
  temp.node = head;
  return temp;
}
Note that you shouldn't be creating anything in this methods.
Thanks it did work for
1
2
list::const_iterator i = s.idList.begin();
list::iterator i = find(idList.begin(),idList.end(),tmp);


However it did not work on the others and made errors such as
1
2
3
4
5
no match for 'operator!=' in 'i != ((list*)s)->list::end()' 
// Which happens 
In function `std::ostream& operator<<(std::ostream&, Statement&)': 
In member function `double Statement::findValue(char*)': 
In member function `void Statement::processNode(char*, double)':  


Canditades to solve the errors are bool Iterator::operator!=(bool)

What does it mean? does it have something to do with the bool error you pointed our earlier?

By doing so, two files which are not included in the project file appear namely stl_algo.h and stl_iterator_base_types.h which from research is an internal header file. Is there any error from the internal header files? This is the first time I've seen them so im very confused on what they do.
Thanks a lot. =) Now I only have 1 error left in the code and it has something to do with stl_algo.h which is an internal header file.

Here is the error.
1
2
3
4
5
6
In function `_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&) [with _InputIterator = Iterator, _Tp = IdNode]':   
instantiated from here //list::iterator i = find(idList.begin(),idList.end(),tmp);  
no matching function for call to `find(Iterator&, Iterator&, const IdNode&, Iterator)'   
candidates are
_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&, std::input_iterator_tag) [with _InputIterator = Iterator, _Tp = IdNode]   
_RandomAccessIterator std::find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = Iterator, _Tp = IdNode] 


Still have no idea on what they mean and how to solve the error.
Again thanks a lot for your help =)
Topic archived. No new replies allowed.