0

2 つのスタックのみを使用してキュー機能を実装するための割り当てに取り組んでいます。私が知る限り、アイデアとコードは完全にダウンしていますが、このエラーが発生しています。私が見たところ、さまざまな .cpp ファイルで発生する 1 つの変数に対して複数の宣言があるためです。このエラーがよくわかりません。私の唯一の推測は、「空」と呼ばれるスタックの関数があり、キューに同じ名前の同様の関数があるということです。誰かが私がどこで間違っているのか説明してもらえますか? 助けてくれてありがとう! 私のコード

編集:

正確なエラー メッセージ:

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-----------------------------------------    

SQueue.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---------------------------------------------------

SQueue.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