2013-01-16 4 views
0

C++ 할당 코드를 작성하고 있는데 Queue.h 및 Queue.cpp 파일이 있습니다. 하지만 main()에 "Queue.h"를 #include했을 때 사용할 수는 없습니다. Queue.h 파일의 12 행에서 위의 문제가 발생했습니다.큐 오류 C2371 : 'ItemType': 재정의; 다른 기본 유형

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

나는 Itemtype으로 RecordService 클래스를 넣기를 원했습니다.

My 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 유 전 "Queue.h"헤더에 나타내고 itemtype로 RecordService을 데려 가고 싶다는 것을 볼 수 있습니다 여기에

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

참고 Queue 클래스 (public를 만든 경우) 외부 Queue 내부와 Queue::ItemType 같은 : [돈 헤더 파일에서'namespace std;를 사용하지 말 것 '(http://stackoverflow.com/questions/5849457/using- C 헤더에있는 네임 스페이스). –

+1

전처리 자 정의에서만 대문자 사용 (Queue_H-> QUEUE_H) – Csq

+0

오류가 main에 있다고 생각하기 시작합니다. – Csq

답변

0

ItemTypeQueue 클래스 내에서 이동할 수 있습니다. 이제 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

+0

안녕하세요,이 솔루션 큐 파일에서 해결할 수 있지만 연결된 목록 (배열) 다른 문제가 있어요. 문제는 클래스 위에 typedef ItemType을 넣을 때 main()에 오류가 발생했습니다. 클래스 밑으로 옮길 때,리스트 cpp 파일에 오류가 있습니다. : – user1498364

+0

구체적인 오류 메시지가 없으면 목록 클래스 외부에있는'ItemType'에 대한 참조가있는 것 같아요. 그러므로 그것들을'List :: ItemType'으로 대체해야합니다. – Csq

관련 문제