stl_algo.h ERRORS !!!

I am writing an interpreter for c++ but I have this error in stl_algo.h
1
2
3
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] 


I have a feeling that this error i caused by #include<algorithm> being commented out in interpreter.h However this error is still present eventhough #include<algoritm> is commented back in the code.

Pls Help.Thanks to those who will help =)

Here is the code of my interpreter:
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
//**************************  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.
}

//**************************  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 != (const Iterator&);
      
      typedef Iterator iterator_category; 
      typedef Iterator value_type;
      typedef Iterator difference_type;
      typedef Iterator pointer;
      typedef Iterator reference;


};

class list
{
      IdNode *head, *tail;
      public:
             typedef Iterator iterator;
             typedef Iterator const_iterator;
             Iterator begin ();
             Iterator end ();
             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();
        for ( ; i != s.idList.end(); i++)
        out << *i;
        out << endl;
        return out;
    }
};

#endif

//**************************  interpreter.cpp   ***********************

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

Iterator list :: begin()
{
iterator temp;
temp.node = head;
return temp;
}

Iterator list :: end()
{
  iterator temp;
  temp.node = tail;
  return temp;
}

Iterator list :: push_front()
{
}

double Statement::findValue(char *id) {
    IdNode tmp(id);
    list::iterator i = find(idList.begin(),idList.end(),tmp);
    if (i != idList.end())
    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(),idList.end(),tmp);
    if (i != idList.end())
    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");
    }
}
You have two problems. The first is easy to fix. The second not so much.

First, your calls to std::find() need to be std::find_if(), I believe.

Second, there is more to making an iterator than simply making the same typedefs in your iterator type
as the STL container iterators have. You need to look at how STL makes iterator types. Unfortunately
the code I have I cannot post, but what it does is it inherits from std::iterator<>, so you might start
by looking at std::iterator<>.
Ok. The problem is solved. However they were replaced by linker errors such as these:
1
2
3
4
5
  [Linker error] undefined reference to `Iterator::operator!=(Iterator const&) const' // appears 4 times
  [Linker error] undefined reference to `Iterator::operator->()' // appears twice
  [Linker error] undefined reference to `Iterator::operator*()' // appears twice
  [Linker error] undefined reference to `Iterator::operator++(int)' 
  [Linker error] undefined reference to `Iterator::operator++()'  


I think this is becasue I have added changes to my Iterator class. Here it is:
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 Iterator
{
      public:
      IdNode *node;
      Iterator &operator ++ ();
      IdNode &operator * ();
      IdNode *operator -> ();
      bool operator != (const Iterator&);
      
      typedef ptrdiff_t                     difference_type;  
      typedef bidirectional_iterator_tag    iterator_category;  
      typedef IdNode                           value_type;  
      typedef IdNode*                          pointer;  
      typedef IdNode&                          reference;    
      
      reference operator*() const;
      pointer operator->() const;
      Iterator& operator++();
      Iterator operator++(int);
      Iterator& operator--();
      Iterator operator--(int);
      bool operator==(const Iterator& __x) const;
      bool operator!=(const Iterator& __x) const;

};


Why does this happen? Is it because the code is split into three parts and the compiler have problems linking all three source code? Will it work when i put all the three source code into just 1?

Please Help. Thanks =)
Are you linking all of your .o (.obj) files into the executable?
Have you implemented the above functions?
Topic archived. No new replies allowed.