繁体   English   中英

队列错误C2371:“ ItemType”:重新定义; 不同的基本类型

[英]Queue error C2371: 'ItemType' : redefinition; different basic types

我正在为C ++分配编写代码,并且有Queue.h和Queue.cpp文件。 但是当我在main()中放入#include“ Queue.h”时,无法使用它们。 我在Queue.h文件的第12行中遇到了上述问题。

error C2371: 'ItemType' : redefinition; different basic types

我想将RecordService类作为Itemtype。

我的Queue.h文件在下面。

#pragma once
#ifndef Queue_H
#define Queue_H
#include<iostream>
#include<string>
#include "RecordService.h"
using namespace std;

typedef RecordService ItemType;  // << problem here

/** ADT queue - Pointer-based implementation. */
class Queue
{
private:
    /** A node on the queue. */ 
    struct Node
    {
        /** A data item on the queue. */
        ItemType item;
        /** Pointer to next node.     */
        Node    *next;
    }; // end Node

    /** Pointer to front node in the queue. */
    Node *frontNode;
    /** Pointer to back node in the queue. */
    Node *backNode;

public:

   /** Default constructor. */
   Queue();

   /** Destructor. */
   ~Queue();

// Queue operations:
   bool isEmpty() const;
   bool enqueue(ItemType& newItem);
   bool dequeue();
   bool dequeue(ItemType& item);
   void getFront(ItemType& item) const;

}; // end Queue
// End of header file.

#endif

我的Queue.cpp在这里

#include "Queue.h"  // header file


Queue::Queue(void)
{   
}  // end default constructor


Queue::~Queue()
{
   while (!isEmpty())
      dequeue();
}  // end destructor

bool Queue::isEmpty() const
{
   return backNode == NULL;
}  // end isEmpty

bool Queue::enqueue(ItemType& newItem)
{
     // create a new node
      Node *newNode = new Node;
      newNode->item = newItem;
      newNode->next = NULL;

      // insert the new node
      if (isEmpty())
     // insertion into empty queue
         frontNode = newNode;
      else
     // insertion into nonempty queue
         backNode->next = newNode;

      backNode = newNode;  // new node is at back
      return true;

}  // end enqueue

bool Queue::dequeue() 
{
   if(isEmpty())
        {
            cout<<"The queue is empty."<<endl;
        }
        else
        {
            Node *temp= frontNode;
            if(frontNode==backNode)
            {
                frontNode=NULL;
                backNode=NULL;
            }
            else
                frontNode=frontNode->next;

            temp->next=NULL;
            delete (temp);
        }
        return true;  // end if

}  // end dequeue

bool Queue::dequeue(ItemType& item)
{
   if(isEmpty())
    {
        cout<<"The queue is empty."<<endl;
    }
    else
    {
        item = frontNode->item;
        dequeue();
    }
    return true;

}  // end dequeue

void Queue::getFront(ItemType& item) const
{
   if (!isEmpty())
      // queue is not empty; retrieve front
      item = frontNode->item;
    else 
        cout << "The queue is empty." << endl;
}  // end getFront

您可能会看到我想将RecordService作为Itemtype放在“ Queue.h”标头中。 这是RecordService.h

#pragma once
#ifndef RecordService_H
#define RecordService_H
#include <iostream>
#include <string>
#include <ctime>
using namespace std;

class RecordService
{
public:
    RecordService();
    RecordService(string, string, string, int, double, bool, string);
    ~RecordService();

    //set methods
    void setTransID(char);
    void setCusName(string);
    void setVehicleNo(string);
    void setCarType(string);
    void setWashDuration(int);
    void setShampooDuration(int);
    void setPolishDuration(int);
    void setVacuumDuration(int);
    void setTotalDuration(int,int,int,int);
    void setWashingCharge(double);
    void setShampooingCharge(double);
    void setPolishingCharge(double);
    void setVacuumingCharge(double);
    void setTotalCharge(double,double,double,double);
    void setRewardStatus(bool);
    void setDateOfTrans(string);

    //get methods
    char getTransID();
    string getCusName();
    string getVehicleNo();
    string getCarType();
    int getWashDuration();
    int getShampooDuration();
    int getPolishDuration();
    int getVacuumDuration();
    int getTotalDuration();
    double getWashingCharge();
    double getShampooingCharge();
    double getPolishingCharge();
    double getVacuumingCharge();
    double getTotalCharge();
    bool getRewardStatus();
    string getDateOfTrans();

private:
    char transID;
    string CusName;
    string vehicleNo;
    string carType;
    int WashDuration;
    int ShampooDuration;
    int PolishDuration;
    int VacuumDuration;
    int TotalDuration;
    double WashingCharge;
    double ShampooingCharge;
    double PolishingCharge;
    double VacuumingCharge;
    double TotalCharge;
    bool RewardStatus;
    string DateOfTrans;

};
#endif

预先感谢帮助我。 由于此错误,我无法继续进行其他操作。

ItemType可以在Queue类内移动。 现在,它可能会与您已定义ItemType其他标头发生冲突。

所以代替

typedef RecordService ItemType;  // << problem here

/** ADT queue - Pointer-based implementation. */
class Queue
{
    ...
}

采用

/** ADT queue - Pointer-based implementation. */
class Queue
{
    typedef RecordService ItemType;

    ...
}

ItemType仍然可以访问作为ItemType内部QueueQueue::ItemTypeQueue (如果对类的public

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM