Error :: while display the Elements inside Template Queue ( usesSTL List )

P

Pallav singh

Hi

i am getting Error while trying to display the Elements stored inside
Template Queue ( which internally uses STL List )

Thanks
Pallav

////////////////////////////////////////////////////////////////////////////////////////

PQueue.h

#include <list.h>
template <class Object>
class PQueue
{
private:
list<Object> queue;

public:
PQueue( );
PQueue( const PQueue & rhs );
~PQueue( );

bool isEmpty( ) const;
bool isFull( ) const;
const Object & top( ) const;

void makeEmpty( );
void pop( );
void push( const Object & x );

void display()const;

list<Object> & getQueue();
};

/////////////////////////////////////////////////////////////////////////////////////////////
PQueue.cc

#include "PQueue.h"
#include <iostream.h>
#include <list.h>

template <class Object>
PQueue<Object>::pQueue( )
{
list<Object> queue;
}

template <class Object>
PQueue<Object>::pQueue( const PQueue<Object> & rhs )
{
queue = rhs.getQueue();
}

template <class Object>
PQueue<Object>::~PQueue( )
{
queue.clear();
}

template <class Object>
list<Object> & PQueue<Object>::getQueue( )
{
return queue;
}

template <class Object>
bool PQueue<Object>:: isEmpty( ) const
{
return ( queue.size() == 0 );
}

template <class Object>
bool PQueue<Object>:: isFull( ) const
{
return false;
}

template <class Object>
const Object & PQueue<Object>::top( ) const
{
queue.front();
}

template <class Object>
void PQueue<Object>::pop( )
{
queue.pop_front();
}

template <class Object>
void PQueue<Object>::push( const Object & x )
{
queue.push_back(x);
}

template <class Object>
void PQueue<Object>::display( )const
{
// list<int>::iterator iter;
list<Object>::iterator iter;
cout << "mylist contains:";
for ( iter = queue.begin() ; iter != queue.end(); iter++ )
cout << " " << *iter << endl;
}

////////////////////////////////////////////////////////////////////////////////////////
TestQueueLi.cc

#include "PQueue.cc"
#include <iostream.h>

int main( )
{
PQueue<int> s;

for( int i = 0; i < 10; i++ )
s.push( i );

s.display();

s.pop();
s.pop();
s.pop();

s.display();

cout << "s" << endl;

return 0;
}

PQueue.cc: In member function `void PQueue<Object>::display() const':
PQueue.cc:69: error: expected `;' before "iter"
PQueue.cc:71: error: `iter' undeclared (first use this function)
PQueue.cc:71: error: (Each undeclared identifier is reported only once
for each function it appears in.)
PQueue.cc: In member function `void PQueue<Object>::display() const
[with Object = int]':
TestQueueLi.cc:11: instantiated from here
PQueue.cc:69: error: dependent-name `
std::list<Object,std::allocator<_CharT> >::iterator' is parsed as a
non-type, but instantiation yields a type
PQueue.cc:69: note: say `typename
std::list<Object,std::allocator<_CharT> >::iterator' if a type is
meant
 
A

Alf P. Steinbach

* Pallav singh:
PQueue.cc: In member function `void PQueue<Object>::display() const
[with Object = int]':
TestQueueLi.cc:11: instantiated from here
PQueue.cc:69: error: dependent-name `
std::list<Object,std::allocator<_CharT> >::iterator' is parsed as a
non-type, but instantiation yields a type
PQueue.cc:69: note: say `typename
std::list<Object,std::allocator<_CharT> >::iterator' if a type is
meant

*Read* those error messages.

Then do as your compiler tells you to.

Note: you'll eventually need to move your implementation code to the header
file, unless you're (only) using a compiler that supports 'export'.


Cheers & hth.,

- Alf

PS: Haven't you asked about this before? Anyway, it's a FAQ item. It's always a
good idea to check the FAQ before posting.
 
R

red floyd

Hi

i am getting Error  while trying to display the Elements stored inside
Template Queue ( which internally uses STL List )

Thanks
Pallav

////////////////////////////////////////////////////////////////////////////////////////

PQueue.h

#include <list.h>
template <class Object>
class PQueue
{
     private:
     list<Object>  queue;

     public:
     PQueue( );
     PQueue( const PQueue & rhs );
    ~PQueue( );

     bool isEmpty( ) const;
     bool isFull( ) const;
     const Object & top( ) const;

     void makeEmpty( );
     void pop( );
     void push( const Object & x );

     void display()const;

     list<Object>  & getQueue();

};
[redacted]

template <class Object>
void  PQueue<Object>::display( )const
{
   // list<int>::iterator iter;
     list<Object>::iterator iter;
typename list said:
     cout << "mylist contains:";
     for ( iter = queue.begin() ; iter != queue.end(); iter++ )
         cout << " " << *iter << endl;

}

You need the typename keyword on line 69. "iterator" is a dependent
name.
See FAQ 35.18 http://parashift.com/c++-faq-lite/templates.html#faq-35.18
 
P

Pallav singh

i am getting Error  while trying to display the Elements stored inside
Template Queue ( which internally uses STL List )



#include <list.h>
template <class Object>
class PQueue
{
     private:
     list<Object>  queue;
     public:
     PQueue( );
     PQueue( const PQueue & rhs );
    ~PQueue( );
     bool isEmpty( ) const;
     bool isFull( ) const;
     const Object & top( ) const;
     void makeEmpty( );
     void pop( );
     void push( const Object & x );
     void display()const;
     list<Object>  & getQueue();
};
[redacted]

template <class Object>
void  PQueue<Object>::display( )const
{
   // list<int>::iterator iter;
     list<Object>::iterator iter;

typename list said:
     cout << "mylist contains:";
     for ( iter = queue.begin() ; iter != queue.end(); iter++ )
         cout << " " << *iter << endl;

You need the typename keyword on line 69.  "iterator" is a dependent
name.
See FAQ 35.18http://parashift.com/c++-faq-lite/templates.html#faq-35.18

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++

typename list<Object>::iterator iter;
for ( iter = queue.begin() ; iter != queue.end(); iter++ ) // getting
Error Here
cout << " " << *iter << endl;

PQueue.cc:64: error: no match for 'operator=' in 'iter = ((const
std::list<int, std::allocator<int> >*)this)->std::list<_Tp,
_Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>]

Thanks
Pallav
()'
 
T

Thomas J. Gritzan

Pallav said:
Hi
i am getting Error while trying to display the Elements stored inside
Template Queue ( which internally uses STL List )
Thanks
Pallav
////////////////////////////////////////////////////////////////////////////////////////
PQueue.h
#include <list.h>
template <class Object>
class PQueue
{
private:
list<Object> queue;
public:
PQueue( );
PQueue( const PQueue & rhs );
~PQueue( );
bool isEmpty( ) const;
bool isFull( ) const;
const Object & top( ) const;
void makeEmpty( );
void pop( );
void push( const Object & x );
void display()const;
list<Object> & getQueue();
};
[redacted]
template <class Object>
void PQueue<Object>::display( )const
{
// list<int>::iterator iter;
list<Object>::iterator iter;
typename list said:
cout << "mylist contains:";
for ( iter = queue.begin() ; iter != queue.end(); iter++ )
cout << " " << *iter << endl;
}
You need the typename keyword on line 69. "iterator" is a dependent
name.
See FAQ 35.18http://parashift.com/c++-faq-lite/templates.html#faq-35.18

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++

typename list<Object>::iterator iter;

Object is a bad name for a template parameter.
for ( iter = queue.begin() ; iter != queue.end(); iter++ ) // getting
Error Here
cout << " " << *iter << endl;

PQueue.cc:64: error: no match for 'operator=' in 'iter = ((const
std::list<int, std::allocator<int> >*)this)->std::list<_Tp,
_Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>]

You can't use an iterator on a const object. Use const_iterator instead:

typename list<Object>::const_iterator iter;
 

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,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top