Trouble with Int Queue

From: Barry & Angela Hynes (base_at_isllandtelecom.com)
Date: 05/24/04

  • Next message: Leor Zolman: "Re: Trouble with Int Queue"
    Date: Mon, 24 May 2004 00:50:16 GMT
    
    

    Good Day Folks,

    The following is the code for an integer queue using a namespace. it compile
    ok but i do not get the output that i expected...any help greatly
    appreciated

    thnaks
    Barry
    //*****************************************************
    #ifndef INTQUEUE_H
    #define INTQUEUE_H

    namespace queueNamespace{
     class IntQueue{
      public:
      IntQueue(int = 20);
      IntQueue(const IntQueue&);
      ~IntQueue();
      IntQueue& operator=(const IntQueue&);
      void enQueue(int) throw (logic_error);
      int deQueue() throw (logic_error);
      int firstInLine() throw (logic_error);
            int size() throw (logic_error);
     private:
      int* queue_;
      int size_;
      int front_;
      int back_;
     };
    }
    #endif
    //****************************************
    //file: intQueue.cpp
    #include <iostream.h>
    using namespace std;
    #include "intQueue.h"
    using namespace queueNamespace;
    //IntQueue class definitions.
      IntQueue::IntQueue(int size){
      queue_ = new int[size];
      size_ = size;
      front_ = 0;
      back_ = 0;
     for(int i = 0; i <= size_; ++i)
     queue_[i] = 0;//initializing array slots to 0
     }

     IntQueue::IntQueue(const IntQueue& s){
      front_ = s.front_;
      back_ = s.back_;
      size_ = s.size_;
      for(int i = 0; i <= size_; ++i)
       queue_[i] = s.queue_[i];
     }

     IntQueue& IntQueue::operator=(const IntQueue& s){
      if(this == &s)
       return *this;

      if(size_ != s.size_){
       delete [] queue_;
      queue_ = new int[s.size_];
      }
      front_ = s.front_;
      back_ = s.back_;
     size_ = s.size_;
      for(int i = 0; i <= size_; ++i)
       queue_[i] = s.queue_[i];
        return *this;
     }

     IntQueue::~IntQueue(){
      delete [] queue_;
     }

     void IntQueue::enQueue(int i) throw (logic_error){
      if(back_ == size_ - 1)
       throw logic_error("Queue Full...");
       queue_[++back_] = i;
     }

     int IntQueue::deQueue() throw (logic_error){
      if(back_ == 0)
       throw logic_error("Queue Empty...");
       return queue_[front_--];
     }

     int IntQueue::firstInLine() throw (logic_error){
      if(back_ == 0)
       throw logic_error("Queue Empty...");
       return queue_[front_];
     }
     int IntQueue::size() throw (logic_error){
       if(size_ < 0)
        throw logic_error("Invalid Size...");
        return size_;
     }
    //****************************************
    #include <iostream.h>
    using namespace std;
    #include "intQueue.h"
    using namespace queueNamespace;
    int main() {
      queueNamespace::IntQueue s;
      try{
            s.enQueue(115);
            s.enQueue(10);
            s.enQueue(15);
            s.enQueue(20);
            s.enQueue(25);
      }
      catch(const bad_alloc&){
              cout << "run out of memory" << endl;
      }
      catch(const logic_error& e) {
              cout << e.what() << endl;
      }

      try{
            for(int x = 0; x <= 5; x++)
              cout << "Slot " << x << ": " << s.deQueue() << endl;
     }
     catch(const bad_alloc&){
              cout << "run out of memory" << endl;
      }
      catch(const logic_error& e) {
              cout << e.what() << endl;
      }
     };

    I added an initialization to the constructor to set all the elements of the
    array to 0...but it does not seem to work.
    only for element 0...not sure why???

    any help greatly appreciated


  • Next message: Leor Zolman: "Re: Trouble with Int Queue"

    Relevant Pages

    • Re: New to C++ entirely (HELP!)
      ... MFC and ATL are C++ class libraries that target native Win32 development. ... int __stdcall WinMain ... using namespace System::IO; ... static void Main2; ...
      (microsoft.public.dotnet.languages.vc)
    • Re: Personal project, extending Scheme to OOP
      ... I've been passing around a reference to a single namespace ... unsigned int type: 4; ...
      (comp.lang.scheme)
    • Re: Late binding with unmanaged code
      ... I wonder can someone else show an example of importing GetProcAddress ... Scott Blood ... I don't know if i am missing a namespace. ... public extern static int InvokeFunc(int funcptr, int hwnd, string ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Q: Why is my BitBlt destination always blank?
      ... the dest bitmap is written to the file system. ... using namespace System; ...
      (microsoft.public.win32.programmer.gdi)
    • Re: unresolved external symbol
      ... Regards, ... > using namespace K1; ... > int APIENTRY _tWinMain(HINSTANCE hInstance, ... >>> a member of the my namespace. ...
      (microsoft.public.dotnet.languages.vc)