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..
carlNYCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

beavis_shenzhenCommented:
Your code worked fine in my vc7.0 and 6.0.
Maybe its some setting errors,such as lib path.
0
efnCommented:
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
0
carlNYCAuthor Commented:
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..
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

efnCommented:
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.
0
carlNYCAuthor Commented:
#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.
0
efnCommented:
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.
0
carlNYCAuthor Commented:

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...
0
carlNYCAuthor Commented:
class ObjectQueue
{
   public:
        void CreateQueue();
        void CheckIfDuplicateAndAdd(Object );

   private:
        list<Object> myList;    
         queue<Object, list<Object> > q(myList);
};
0
efnCommented:
Now I'm confused.  Is the current problem a compile-time error complaining about myList not being a class, or is it a run-time problem that occurs when the program tries to construct q?

Anyway, what you have will not do what you want.  q is not a wrapper on myList--it does not contain myList.  When you construct q from myList, q gets a copy of myList's data, and the two copies are thereafter independent.  So unless you painstakingly maintain the same data in q and myList at all times (which your function doesn't do), you can't search myList to find out if something is already in q.

A queue isn't really a good data structure for searching.  You could make a copy of your queue and pop objects from the copy to search it.  Or you could use a deque, which has random access iterators.

--efn
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
carlNYCAuthor Commented:
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 ..
0
efnCommented:
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
0
ivecCommented:
> 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.
0
hlideCommented:

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


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.