2013-01-16 31 views
0

我正在編寫C++代碼,我有Queue.h和Queue.cpp文件。但是當我在main()中放入#include「Queue.h」時不能使用它們。我在Queue.h文件的第12行中得到了上述問題。隊列錯誤C2371:'ItemType':重新定義;不同的基本類型

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 

U可以看到,我想提出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 

提前感謝幫助我。由於這個錯誤,我不能移動到其他部分。

+1

FYI外:唐'在名稱空間中使用'namespace std;'](http://stackoverflow.com/questions/5849457/using-命名空間中-C-頭)。 –

+1

僅在預處理器定義中使用大寫字母(Queue_H-> QUEUE_H) – Csq

+0

我開始認爲錯誤主要在於。 – Csq

回答

0

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仍然可以訪問爲ItemTypeQueueQueue::ItemTypeQueue類(如果由public

+0

你好,我可以解決這個解決方案的隊列文件,但我有鏈接列表(數組)的另一個問題。問題是當我把typedef ItemType放在類的上面時,我在main()中出錯。當我在課堂上移動它時,我得到了列表cpp文件中的錯誤。 :( – user1498364

+0

沒有具體的錯誤消息我想你有列表類以外的ItemType的引用,因此你必須用List :: ItemType替換它們。 – Csq

相關問題