为什么这个typedef不起作用

时间:2011-10-23 23:53:30

标签: c++ typedef

如果第7行显示为“typedef char ItemType”,则此代码有效,但我将其设置为EventInfo对象的typedef。 MSVS编译器说了一些非常奇怪的事情......

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();

}

4 个答案:

答案 0 :(得分:2)

在C ++中,与Java不同,您只能使用文件中已声明之前的名称。所以只需更改顺序:

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

事实上,如果您还不想定义它,只需声明类就足够了:class EventInfo;

答案 1 :(得分:2)

名称EventInfo仅可从其声明转发点开始使用。你的typedef太早了,编译器还不知道EventInfo是什么。

您可以将typedef移动到类定义下面,或者您可以告诉编译器EventInfo是一个类:

typedef class EventInfo ItemType;

答案 2 :(得分:1)

将typedef放在类定义之后。你不能为未知类型做typedef。

答案 3 :(得分:1)

首先,您需要转发声明class EventInfo;

否则你的typedef不知道EventInfo是什么。

你有各种各样的问题,例如你需要重载&lt;&lt;对于EventInfo类等。