0

我正在研究仅使用 2 个堆栈来实现队列功能的任务。据我所知,我完全有这个想法和代码,但我收到了这个错误。从我所看到的来看,这是因为在各种 .cpp 文件中出现了 1 个变量的多个声明。我不太了解这个错误。我唯一的猜测是我有一个名为“empty”的堆栈函数,并且我在队列中有一个类似的同名函数。有人可以向我解释我哪里出错了吗?谢谢你的帮助! 我的代码

编辑:

确切的错误信息:

SQueueMain.obj : error LNK2005: "public: __thiscall Stack::Stack(void)" (??0Stack@@QAE@XZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: __thiscall Stack::Stack(class Stack const &)" (??0Stack@@QAE@ABV0@@Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: __thiscall Stack::~Stack(void)" (??1Stack@@QAE@XZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: class Stack const & __thiscall Stack::operator=(class Stack const &)" (??4Stack@@QAEABV0@ABV0@@Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: bool __thiscall Stack::empty(void)const " (?empty@Stack@@QBE_NXZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall Stack::push(int const &)" (?push@Stack@@QAEXABH@Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall Stack::display(class std::basic_ostream<char,struct std::char_traits<char> > &)const " (?display@Stack@@QBEXAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: int __thiscall Stack::top(void)const " (?top@Stack@@QBEHXZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall Stack::pop(void)" (?pop@Stack@@QAEXXZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: __thiscall queue::queue(void)" (??0queue@@QAE@XZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: __thiscall queue::~queue(void)" (??1queue@@QAE@XZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall queue::enqueue(int)" (?enqueue@queue@@QAEXH@Z) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall queue::dequeue(void)" (?dequeue@queue@@QAEXXZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: int __thiscall queue::front(void)" (?front@queue@@QAEHXZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: bool __thiscall queue::empty(void)" (?empty@queue@@QAE_NXZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "private: class Stack::Node * __thiscall queue::setFront(void)" (?setFront@queue@@AAEPAVNode@Stack@@XZ) already defined in SQueue.obj

SQueueMain.cpp

//SQueueMain.cpp starts here---------------------------------------
#include "SQueue.cpp"

int main()
{
    queue queue1;
int switchNum, tempNum;
while (1)
{
    cout << "What action would you like to perform? \n";
    cout << "Please enter the number that corresponds with the correct action.              \n";
    cout << "0: Enqueue an item. \n";
    cout << "1: Dequeue an item. \n";
    cout << "2: Return the front value. \n";
    cout << "3: Determine if the queue is empty. \n";
    cout << "4: Exit the program. \n";
    cin >> switchNum;

    switch (switchNum)
    {
    case 0:
        {
            cout << "What is the value you wish to enqueue? \n";
            cin >> tempNum; 
            queue1.enqueue(tempNum);
        }
        break;
    case 1:
        {
            queue1.dequeue();
        }
        break;
    case 2:
        {
            cout << queue1.front();
        }
        break;
    case 3:
        {
            cout << queue1.empty();
        }
        break;
    case 4:
        {
            return 0;
        }
    }
    }
}
//SQueueMain.cpp ends here-----------------------------------------    

队列.cpp

//SQueue.cpp starts here---------------------------------------------------
#include "SQueue.h"
#include <new>
using namespace std;

queue::queue() : myFront(0) {}
queue::~queue()
{
    queue::s1.~Stack(); 
    queue::s2.~Stack();
}
void queue::enqueue(int num)
{
    queue::s1.push(num);
}
void queue::dequeue()
{
    queue::setFront();
    queue::s2.pop();
}
queueElement queue::front()
{
    queue::setFront();
    if (!empty())
        return (myFront->data);
    else
    {
        cerr << "*** Queue is empty -- returing garbage ***\n";
        return *(new queueElement);
    }
}
bool queue::empty()
{
    queue::setFront();
    return (myFront == 0);
}
Stack::NodePointer queue::setFront()
{
int temp; 
    if (queue::s2.empty() == true)
    {
        while (queue::s1.empty() != true)
        {
            temp = queue::s1.myTop->data;
            queue::s1.pop(); 
            queue::s2.push(temp);
        }
    }
    myFront = queue::s2.myTop;
    return myFront;
}
//SQueue.cpp ends here---------------------------------------------------

队列.h

//SQueue.h starts here-------------------------------------------------------------   
#include <iostream>
#include "LSTack.cpp"
using namespace std; 
#ifndef SQUEUE
#define SQUEUE
typedef int queueElement;

class queue
{
public:
    Stack s1, s2; 
    queue();
    ~queue();
    void enqueue(int); 
    void dequeue(); 
    queueElement front();
    bool empty();
    Stack::NodePointer myFront; //typedef from LStack.h
private:
    Stack::NodePointer setFront();

};
#endif
//SQueue.h ends here--------------------------------------------------------------

LStack.cpp

//--- LStack.cpp -------------------------------------------------

#include <new>
using namespace std;

#include "LStack.h"

//--- Definition of Stack constructor
Stack::Stack(): myTop(0){} 

//--- Definition of Stack copy constructor
Stack::Stack(const Stack & original)
{
   myTop = 0;
   if (!original.empty())
   {
      // Copy first node
      myTop = new Stack::Node(original.top());

      // Set pointers to run through the stack's linked lists
      Stack::NodePointer lastPtr = myTop,
                         origPtr = original.myTop->next;

      while (origPtr != 0)
      {
         lastPtr->next = new Stack::Node(origPtr->data);
         lastPtr = lastPtr->next;
         origPtr = origPtr->next;
      }
   }
}

//--- Definition of Stack destructor
Stack::~Stack()
{     
   // Set pointers to run through the stack
   Stack::NodePointer currPtr = myTop,  // node to be deallocated
                      nextPtr;          // its successor
   while (currPtr != 0)
   {
      nextPtr = currPtr->next;
      delete currPtr;
      currPtr = nextPtr;
   }
}

//--- Definition of assignment operator
const Stack & Stack::operator=(const Stack & rightHandSide)
{
   myTop = 0;
   if (rightHandSide.empty()) return *this;

   if (this != & rightHandSide)     // check that not st = st
   {
      this->~Stack();         // destroy current linked list

      // Copy first node
      myTop = new Stack::Node(rightHandSide.top());

      // Set pointers to run through the stacks' linked lists
      Stack::NodePointer lastPtr = myTop,
                         rhsPtr = rightHandSide.myTop->next;

      while (rhsPtr != 0)
        {
          lastPtr->next = new Stack::Node(rhsPtr->data);
          lastPtr = lastPtr->next;
          rhsPtr = rhsPtr->next;
        } 
  }
   return *this;
}

//--- Definition of empty()
bool Stack::empty() const
{ 
   return (myTop == 0); 
}

//--- Definition of push()
void Stack::push(const StackElement & value)
{
   myTop = new Stack::Node(value, myTop);
}

//--- Definition of display()
void Stack::display(ostream & out) const
{
   Stack::NodePointer ptr;
   for (ptr = myTop; ptr != 0; ptr = ptr->next)
      out << ptr->data << endl;
}

//--- Definition of top()
StackElement Stack::top() const
{
   if (!empty())
      return (myTop->data);
   else
   {
      cerr << "*** Stack is empty "
              " -- returning garbage ***\n";
      return *(new StackElement);   // "Garbage" value
   }
}

//--- Definition of pop()
void Stack::pop()
{
   if (!empty())
   {
      Stack::NodePointer ptr = myTop;
      myTop = myTop->next;
      delete ptr;
   }   
   else
      cerr << "*** Stack is empty -- can't remove a value ***\n";
}

LStack.h

#include <iostream>
using namespace std; 
#ifndef LSTACK
#define LSTACK

typedef int StackElement; 
class Stack
{
    friend class queue;
 public:
     Stack(); 
     Stack(const Stack & original); 
 ~Stack();
 const Stack & operator = (const Stack & rightHandSide);
 bool empty() const; 
 void push(const StackElement & value);
 void display(ostream & out) const; 
 StackElement top() const;
 void pop(); 

 /***** Function Members *****/
 // Prototypes same as in preceding section

 private:
    /*** Node class ***/
   class Node
   {
    public:
      StackElement data;
      Node * next;
      //--- Node constructor
      Node(StackElement value, Node * link = 0)
      /*------------------------------------------------------
        Precondition:  value is received
        Postcondition: A Node has been constructed with value
            in its data part and itb next part set to link
            (default 0).
       ------------------------------------------------------*/
      { data = value; next = link; }

  };

  typedef Node * NodePointer;

  /***** Data Members *****/
  NodePointer myTop;      // pointer to top of stack

 }; // end of class declaration 
#endif
4

1 回答 1

0
#include "SQueue.cpp"  // KILL THIS or exclude SQueue.cpp in compilation process
于 2013-07-11T04:03:50.603 回答