Link to home
Start Free TrialLog in
Avatar of carlNYC
carlNYC

asked on

STL list based queue

Hi guys,
I have a big problem.. I am using VC++ 6.0 copyright 94-98 Microsoft corporation..
There is some bug in my compiler that prevents me to finish my project. What i am trying to so is to create a STL linked list-based queue..Here  i created a sample program. I belive that if i make it working on this one it will work on the other one..This program will work on any other compiler but not on mine. I can't switch the compiler because i am in middle of another much bigger project. The only one thing i can think about is to upgrade or repair my own compiler. Unfortunately, i could not find anything on Internet to make it working..

#include <list>
#include <queue>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    list<int> myList;
       
    for(int j=1;j<5;++j)myList.push_back(j);

    queue<int, list<int> > myQueue(myList);


          while (!myQueue.empty())
          {
               cout << myQueue.front() << endl;
               myQueue.pop();

          }

    cout << "\n";
     return 0;
}
//The program must output :
//1
//2
//3
//4

I appreciate any help..
Thanks in advance..
Avatar of beavis_shenzhen
beavis_shenzhen

Your code worked fine in my vc7.0 and 6.0.
Maybe its some setting errors,such as lib path.
If you dare to edit the queue header file, you can add the required constructor:

     explicit queue(const _C& initializer)
          : c(initializer) {}

The file should be in Microsoft Visual Studio\Vc98\Include.

--efn
Avatar of carlNYC

ASKER

efn,
I did that and it worked for the sample progtam, but it still does not work on my program. Do you think i may have a logical error or it could be because of my compiler..

private:
     list<Object> myList;              
      queue<Object, list<Object> > q(myList);

// error C2061: syntax error : identifier 'myList'
// error C2228: left of '.push' must have class/struct/union type
//'.empty' must have class/struct/union type
//fatal error C1903: unable to recover from previous error(s); stopping compilation

These are the error messages i get when i use the queue STL .front() , push(), empty()...
Once again, i am trying to create a linked list queue by using STL list and queue..
The error messages indicate that the compiler does not recognize myList as a list.  I can't tell from what you posted why that is.  It might be a helpful exercise to (again) try to construct a minimal program that demonstrates the problem.

If you post more code, I may be able to advise you further.
Avatar of carlNYC

ASKER

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<list>
#include<queue>

#include "ObjectData.h"  
  //contains a class Object
  //has a function that returns a vector of objects

using namespace std;

class QueueData
{
     public:
          void CreateQueue();
          void CheckIfDuplicateAndAdd(Object );

     private:
          list<Object> myList;    
           queue<Object, list<Object> > q(myList);
};


void ObjectQueue::CheckIfDuplicateAndAdd(Object aObject)
{
     list <Object>::iterator i=myList.begin();
     bool ObjectFound=false;

     if(q.front()=q.back())
          q.push(aObject);
         else
      {
         for (i = myList.begin(); i != aList.end(); ++i)
         {
            if(aObject.getValue()!=(*i).getValue()
            ObjecttFound=true;
          }
     }

     if(ObjectFound==true)
           q.push(aObject);
}

Thats a little sample program. It could some little errors, but in general thats the key function which i want implement.
The function shown is a member of ObjectQueue.  Is it supposed to be a member of QueueData?

The if-conditions with one equals sign should probably have two, for comparison rather than assignment.

The logic of the function seems to be:

if (q is empty OR aObject is in myList)
then add aObject to q

Is this what you intended?  It looks a bit suspicious.
Avatar of carlNYC

ASKER


class QueueQueue
{
    public:
         void CreateQueue();
         void CheckIfDuplicateAndAdd(Object );

    private:
         list<Object> myList;    
          queue<Object, list<Object> > q(myList);
};


void ObjectQueue::CheckIfDuplicateAndAdd(Object aObject)
{
    list <Object>::iterator i=myList.begin();
    bool ObjectFound=false;

    if(q.front()==q.back())
         q.push(aObject);
        else
     {
        for (i = myList.begin(); i != aList.end(); ++i)
        {
           if(aObject.getValue()!=(*i).getValue()
           ObjecttFound=true;
         }
    }

    if(ObjectFound==true)
          q.push(aObject);
}

yes.. in general i am trying to add an object if only if its not found..THat's just a sample program , i didnt test but the point is that i am trying to create queue based on STL list..Every time when i try to run my program i get a error at  queue<Object, list<Object> > q(myList); However, if i just do-> queue<Object> myQueue;
and enqueue the vector into myQueue, its gonna work which means that i dont have errors for the rest of my program...
However,in that way i cant traverse the queue and therefore, i cant imlpement CheckIfDuplicateAndAdd...
Avatar of carlNYC

ASKER

class ObjectQueue
{
   public:
        void CreateQueue();
        void CheckIfDuplicateAndAdd(Object );

   private:
        list<Object> myList;    
         queue<Object, list<Object> > q(myList);
};
ASKER CERTIFIED SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of carlNYC

ASKER

efn,
maybe i am just totally wrong..Could you please give me some explanation on what is pointer-based queues and STL. thats excatcly what i am trying to do ..
Hi Carl,

I can't tell if you are trying to use this queue container with a list implementation because you think it's the best choice to accomplish the job or because you are forced to use it for some other reason, like it's required for an assignment.  I'll give you some general information in the hope that it will help.

A queue is appropriate when you want to store a bunch of objects for a while and take them out in the order in which they went in.  Its interface is intentionally limited so that is all you can do with it.

I'm not sure what "pointer-based" means when applied to a queue.  I did some web searching and the term seemed to be used with (at least) two different meanings:

a.  The queue contains pointers to objects that presumably have a lifetime independent of their pointers and the queue.  You might use this kind of queue if you had a bunch of objects making requests and you wanted to serve the requests first-come, first-served, without creating or destroying any of the objects.  It's particularly helpful to use pointers when the objects are big or otherwise expensive or time-consuming to construct or copy.  With this meaning, "pointer-based" is contrasted with "value-based."

b.  The other meaning is that the queue's internal storage mechanism uses pointers to keep track of dynamically allocated elements, contrasted with an array-based queue.

If your main concern with this data structure is efficiently keeping track of a number of objects and not allowing duplication, I'd suggest you use a set.  If you want to do that, but you also need to remember in what order elements went in, so you can take them out in the same order, I'd suggest you use a deque.

If you use either of these, you don't have to write code to search the container yourself.  The standard library has functions that can do that for you.  However, that isn't necessarily easier, because you may have to deal with mysterious things like binary predicate objects.

Finally, I highly recommend the book "The C++ Standard Library" by Nicolai M. Josuttis, published by Addison-Wesley.

HTH,

--efn
> list<int> myList;
> queue<int, list<int> > myQueue(myList);

I do not think this does what you expect.
The declarations above create two *intependent* instances of std::list:
 - one is myList
 - the other is myQueue, which is *initialized* with myList. But onces its contents have been copied from myList, it evolves independently.
Basically, after these objects have been created, the items that are added to myList will NOT be found in myQueue.

Unless that behavior is what you want, you should either:
 - use a single object of type queue<int, list<int> >, and use its push(), pop() and front() routines.
 - use a single object of type list<int>, and use its front(), pop_front() and push_back() routines.

The second option is probably what you need.

An std::list can directly be used as a queue. The only purpose of std::queue is to restrict the interface of the wrapped container to the minimal interface required by a queue.

Your example :

class QueueData
{
    public:
         void CreateQueue();
         void CheckIfDuplicateAndAdd(Object );

    private:
         list<Object> myList;    
           queue<Object, list<Object> > q(myList);
};

What I see is a definition of a class called QueueData, with two private members :

list< Object > myList;
queue< Object, list< Object > > q;

But the second member doesn't look like a member because of your argument (myList).

In fact, you are writing a function q which returns an object of type queue< Object, list< Object > > and has a parameter myList which should be a type.

However, you define myList as being a member, not a type ! so you get the error compilation.


/hlide