QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: bing

g++无法编译通过但是VC6编译通过的程序,请各位指教

[复制链接]
发表于 2004-4-26 14:27:45 | 显示全部楼层
xcept.h
[code:1]// exception classes for various error types

#ifndef Xcept_
#define Xcept_

//#include <except.h>
#include <new>

// bad initializers
class BadInitializers {
   public:
      BadInitializers() {}
};

// insufficient memory
class NoMem {
   public:
      NoMem() {}
};

// change new to throw NoMem instead of xalloc
void my_new_handler()
{
   throw NoMem();
};

std::new_handler Old_Handler_ = std::set_new_handler(my_new_handler);

// improper array, find, insert, or delete index
// or deletion from empty structure
class OutOfBounds {
   public:
      OutOfBounds() {}
};

// use when operands should have matching size
class SizeMismatch {
   public:
      SizeMismatch() {}
};

// use when zero was expected
class MustBeZero {
   public:
      MustBeZero() {}
};

// use when zero was expected
class BadInput {
   public:
      BadInput() {}
};

#endif
[/code:1]
回复

使用道具 举报

发表于 2004-4-26 14:30:05 | 显示全部楼层
[code:1]// header file lqueue.h
// linked queue

#ifndef LinkedQueue_
#define LinkedQueue_
#include <iostream>
#include "node.h"
#include "xcept.h"

template<class T>
class LinkedQueue {
// FIFO objects
     template <class S> friend std::ostream& operator<<(std::ostream&, const LinkedQueue<S>&);
     template <class S> friend std::istream& operator>>(std::istream&, LinkedQueue<S>&);
   public:
      LinkedQueue() {front = rear = 0;} // constructor
      ~LinkedQueue(); // destructor
      bool IsEmpty() const
           {return ((front) ? false : true);}
      bool IsFull() const;
      T First() const; // return first element
      T Last() const; // return last element
      LinkedQueue<T>& Add(const T& x);
      LinkedQueue<T>& Delete(T& x);
      int Size() const;
   private:
      Node<T> *front;  // pointer to first node
      Node<T> *rear;   // pointer to last node
      void Erase();    // delete all nodes
};

template<class T>
LinkedQueue<T>::~LinkedQueue()
{// Queue destructor.  Delete all nodes.
   Node<T> *next;
   while (front) {
      next = front->link;
      delete front;
      front = next;
      }
}

template<class T>
bool LinkedQueue<T>::IsFull() const
{// Is the queue full?
   Node<T> *p;
   try {p = new Node<T>;
        delete p;
        return false;}
   catch (NoMem) {return true;}
}

template<class T>
T LinkedQueue<T>::First() const
{// Return first element of queue.  Throw
// OutOfBounds exception if the queue is empty.
   if (IsEmpty()) throw OutOfBounds();
   return front->data;
}

template<class T>
T LinkedQueue<T>::Last() const
{// Return last element of queue.  Throw
// OutOfBounds exception if the queue is empty.
   if (IsEmpty()) throw OutOfBounds();
   return rear->data;
}

template<class T>
LinkedQueue<T>& LinkedQueue<T>::Add(const T& x)
{// Add x to rear of queue.  Do not catch
// possible NoMem exception thrown by new.

   // create node for new element
   Node<T> *p = new Node<T>;
   p->data = x;
   p->link = 0;

   // add new node to rear of queue
   if (front) rear->link = p;  // queue not empty
   else front = p;             // queue empty
   rear = p;

   return *this;
}

template<class T>
LinkedQueue<T>& LinkedQueue<T>::Delete(T& x)
{// Delete first element and put it in x.  Throw
// OutOfBounds exception if the queue is empty.

   if (IsEmpty()) throw OutOfBounds();

   // save element in first node
   x = front->data;

   // delete first node
   Node<T> *p = front;
   front = front->link;
   delete p;

   return *this;
}

template<class T>
void LinkedQueue<T>::Erase()
{// Delete all nodes.

   Node<T> *next;

   while (front) {
      next = front->link;
      delete front;
      front = next;
      }
}

template<class T>
int LinkedQueue<T>::Size() const
{// Return number of elements in the queue.

   int size = 0;
   Node<T> *current = front;
   while (current) {
      size++;
      current = current->link;
      }

      return size;
}

// overload >>
template<class S>
std::istream& operator>>(std::istream& in, LinkedQueue<S>& q)
{// Input queue q from front to rear.
   // first delete all elements from q

   q.Erase();

   // determine size of input queue
   int size;
   std::cout << "Enter size of queue" << std::endl;
   in >> size;

   if (!size) // no elements
      return in;

   // at least one element to input
   // input the queue elements and link together
   std::cout << "Enter the elements from front to rear"
        << std::endl;

   // get first element
   q.rear = q.front = new Node<S>;
   in >> q.front->data;

   // get remaining elements
   for (int i = 2; i <= size ; i++) {
      q.rear->link = new Node<S>;
      q.rear = q.rear->link;
      in >> q.rear->data;
      }

   // set pointer in rear node
   q.rear->link = 0;

   return in;
}

// overload <<
template <class S>
std::ostream& operator<<(std::ostream& out,const LinkedQueue<S>& q)
{// Output the queue q from front to rear
// output queue size

   int size = q.Size();
   out << "The queue has " << size
       << " element(s)" << std::endl;

   // output queue elements
   out << "The element(s) from front to rear are"
       << std::endl;

   Node<S> *current = q.front;
   for (int i = 1; i <= size ; i++) {
      out << current->data << ' ';
      current = current->link;
      }
   out << std::endl;

   return out;
}


#endif

[/code:1]
回复

使用道具 举报

发表于 2004-4-26 14:35:39 | 显示全部楼层
改了#include <iostream> 和 #include <new> ,加上名字空间std::,和改了几处函数申明后用g++ -Wall lqueue.cpp -o lqueue.exe 编译无错,并可以执行。(我用的是MinGW)。
回复

使用道具 举报

 楼主| 发表于 2004-4-26 23:25:59 | 显示全部楼层
谢谢了!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-8 11:56 , Processed in 0.053827 second(s), 12 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表