?
Solved

STL list based queue

Posted on 2003-03-30
13
Medium Priority
?
476 Views
Last Modified: 2013-12-14
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..
0
Comment
Question by:carlNYC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 1

Expert Comment

by:beavis_shenzhen
ID: 8235673
Your code worked fine in my vc7.0 and 6.0.
Maybe its some setting errors,such as lib path.
0
 
LVL 15

Expert Comment

by:efn
ID: 8235679
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
 

Author Comment

by:carlNYC
ID: 8235785
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:efn
ID: 8235870
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
 

Author Comment

by:carlNYC
ID: 8235956
#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
 
LVL 15

Expert Comment

by:efn
ID: 8236035
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
 

Author Comment

by:carlNYC
ID: 8236076

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
 

Author Comment

by:carlNYC
ID: 8236080
class ObjectQueue
{
   public:
        void CreateQueue();
        void CheckIfDuplicateAndAdd(Object );

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

Accepted Solution

by:
efn earned 240 total points
ID: 8236147
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
 

Author Comment

by:carlNYC
ID: 8236427
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
 
LVL 15

Expert Comment

by:efn
ID: 8236771
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
 
LVL 1

Expert Comment

by:ivec
ID: 8236974
> 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
 

Expert Comment

by:hlide
ID: 8615006

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question