On templates and not declared

A

Andrea Crotti

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---
 
A

Andrea Crotti

Leigh Johnston said:
Try "typename std::deque<T>::iterator it;"

Also do you have to keep doing the "---cut here---" thing? It is both
distracting and incredibly annoying.

/Leigh

/Leigh

Ah great it works!
But why I still don't get it?

Ok I'll avoid the "--cut here--", I thought it was better to separate
code and it's nicely done and seen by emacs+gnus, but I will stop using
it...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top