2011-10-23 63 views
0

此代碼的工作,如果七號線下稱「typedef的燒焦的ItemType」,而是我把它的typedef的EventInfo對象。該MSVS編譯器說一些很奇怪的事情......爲什麼不是這樣的typedef工作

error C2146: syntax error : missing ';' before identifier 'ItemType'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

但我不明白爲什麼它正常工作時,它是一個char

我知道這是一個很大的代碼,但typedef的是7號線。我把整件事包括進來,因爲我不知道會發生什麼。

#include <iostream> 
#include <cstdlib> 
#include <fstream> 
using namespace std; 

const int MaxQueueSize = 8; // Queue Struct can hold up to 8 
typedef EventInfo ItemType; // the queue's data type !DOES NOT COMPILE 

enum EventType {Arrival, Depart}; 
class EventInfo 
{ 
public: 
    EventInfo() : eTime(0), aTime(0), minutes(0) {} 
    int eventTime(); 
    int duration(); 
    void ScheduleEvent(int eTime, int duration); 
    bool compare(int eTime); 
private: 
    int eTime; //either the arrival time of the next customer or the departure time of the customer currently at the teller window 
    int aTime; //either the service time for the arriving customer or the wait time for the customer at the teller window 
    float minutes; 
}; 
int EventInfo::eventTime() 
{ 
    return this->eTime; 
} 
int EventInfo::duration() 
{ 
    return this->aTime; 
} 
void EventInfo::ScheduleEvent(int eTime, int duration) 
{ 
    this->eTime = eTime; 
    this->aTime = duration; 
} 
bool EventInfo::compare(int eTime) 
{ 
    return (eTime == this->eTime); 
} 

/////////////////////////////////////////////////////////////// 
////////////////////////////////////////////////////////////// 
////////////////////////// 
//////////////// 

class CPPQueue 
{ 
public: 
    CPPQueue() : front(0), rear(0), count(0) { }; 
    ItemType item[MaxQueueSize]; 
    bool IsEmpty() const; 
    bool IsFull() const; 
    void Enqueue(ItemType newItem); 
    void PrintQ(); 
    void PrintQueueInfo(); 
    ItemType Dequeue(); 
    int Count(); 
private: 
    int front, rear; 
    int count; 
}; 
bool CPPQueue::IsEmpty() const 
{ 
    return (this->count == 0); 
} 
bool CPPQueue::IsFull() const 
{ 
    return (this->count == MaxQueueSize); 
} 
void CPPQueue::Enqueue(ItemType newItem) 
{ 
    if(this->count == MaxQueueSize) 
    { 
     cerr << "Error! Queue is full, cannot enqueue item.\n" << endl; 
     exit(1); 
    } 
    this->item[this->rear] = newItem; 
    this->rear++; 
    if (this->rear == MaxQueueSize) 
    { 
     this->rear = 0; // adjustment for circular queue 
    } 
    this->count++; 
} 
ItemType CPPQueue::Dequeue() 
{ 
    ItemType theItem; 
    if(this->count == 0) 
    { 
     cerr << "Error! Queue is empty, cannot dequeue item.\n" << endl; 
     exit(1); 
    } 
    theItem = this->item[this->front ]; 
    this->front++; 
    if (this->front == MaxQueueSize) 
    { 
     this->front = 0; // adjustment for circular queue 
    } 
    this->count--; 
    return theItem; 
} 
// Function PrintQ() prints the contents of the queue without changing 
// the queue. Printing starts at the "front" index and stops before we 
// get to the "rear" index. A decrementing counter controls the loop. 
// 
void CPPQueue::PrintQ() 
{ 
    int i; 
    int qindex = this->front; 
    for(i = this->count; i > 0; i--) 
    { 
     cout << this->item[qindex] ; 
     qindex = (++qindex) % MaxQueueSize; // adjustment for circular queue 
     if(i > 1) 
      cout << ", "; 
    } 
} 
// Helper function for the main program below. 
void CPPQueue::PrintQueueInfo() 
{ 
    cout << "The queue contains: "; 
    PrintQ(); 
    cout << endl; 
} 
int CPPQueue::Count() 
{ 
    return this->count; 
} 


enum TellerStatus {Idle, Busy}; 
class Teller 
{ 
public: 
    Teller() : status(Idle), idleTime(0), totalIdleTime(0) {} 
    void changeStatus(TellerStatus status); 
    TellerStatus getStatus(void); 
private: 
    TellerStatus status; 
    int idleTime; //! 
    int totalIdleTime; //!! 
}; 
void Teller::changeStatus(TellerStatus status) 
{ 
    this->status = status; 
} 
TellerStatus Teller::getStatus() 
{ 
    return this->status; 
} 

class Bank 
{ 
public: 
    Bank() : Clock(0.0) {} 
    void RunSimulation(); 
private: 
    EventInfo Event[2]; // array of two events - next arrival and next departure 
    CPPQueue WaitLine; // the customer wait line [with max size = 8] 
    float Clock; // to keep track of Current Bank Time 
    Teller theTeller; // the single teller in the bank 
    ifstream myfile; 
    void ProcessArrival(), ProcessDeparture(), PrintHeader(), PrintReportLine(), PrintStatsReport(); 
}; 
void Bank::RunSimulation() 
{ 

} 
void Bank::ProcessArrival() 
{ 
    int a, b; 
    string filename, x; 
    filename = "P3Data1.txt"; 
    myfile.open(filename); 
    while (myfile >> a >> b) 
    { 
     Event[1].ScheduleEvent(a, b); 
     WaitLine.Enqueue(Event); 
    } 
} 

int main() 
{ 
    Bank myBank; 
    myBank.RunSimulation(); 

} 

回答

2

名稱EventInfo是唯一可用其宣言要點。你的typedef爲時尚早,編譯器還不知道EventInfo是什麼。

可以移動的typedef類定義的下方,或者你可以告訴編譯器EventInfo是一個類:

typedef class EventInfo ItemType; 
2

在C++中,不像在Java中,你只能使用已宣告文件中較早名。因此,只要改變順序:

class EventInfo { /* ... */ }; 
typedef EventInfo ItemType; 

事實上,這是不夠的,只是聲明類,如果你不想去定義它只是尚未:class EventInfo;

1

把typedef的類定義後。你不能對未知類型進行typedef。

1

首先,您需要轉發聲明class EventInfo;

否則你的typedef沒有的EventInfo是什麼想法。

如果您有各種問題,例如您需要爲EventInfo類等過載< <等。