On templates and not declared
I would really like to know what's wrong with the following function
(defined in the class pasted below)
--8<---------------cut here---------------start------------->8---
bool member(T el) {
std::deque<T>::iterator it;
for (it=buffer.begin(); it!=buffer.end(); ++it) {
if ((*it) == el)
return true;
}
return false;
}
--8<---------------cut here---------------end--------------->8---
the problem is generated by the template parameter T, which is used
without any problems in other functions.
If I change T to "int" for example everything works fine, but I do need
that template parameter...
What's the difference with the other classes?
I get a nice
--8<---------------cut here---------------start------------->8---
RingBuffer.hpp: In member function ???bool RingBuffer<T>::member(T)???:
RingBuffer.hpp:28: error: expected `;' before ???it???
RingBuffer.hpp:29: error: ???it??? was not declared in this scope
--8<---------------cut here---------------end--------------->8---
and here the whole class:
--8<---------------cut here---------------start------------->8---
template<typename T>
class RingBuffer
{
private:
size_t max_size;
protected:
std::deque<T> buffer;
public:
RingBuffer() : max_size(0) {};
RingBuffer(size_t max_size) : max_size(max_size) {};
void push(T el) {
if (buffer.size() == max_size) {
buffer.pop_front();
}
buffer.push_back(el);
}
void setSize(size_t max_size) { max_size = max_size; }
T operator[](int index) { return buffer[index]; }
// usual short circuiting algorithm to check for membership
bool member(T el) {
std::deque<T>::iterator it;
for (it=buffer.begin(); it!=buffer.end(); ++it) {
if ((*it) == el)
return true;
}
return false;
}
};
--8<---------------cut here---------------end--------------->8---