为什么这个 typedef 不起作用 [英] Why isn't this typedef working
问题描述
如果第 7 行向下显示typedef char ItemType",则此代码有效,但我将其 typedef 设置为 EventInfo 对象.MSVS 编译器说了一些非常奇怪的事情......
This code works if the 7th line down says "typedef char ItemType" but instead I made it typedef to the EventInfo object. The MSVS compiler says some really weird things...
error C2146:语法错误:缺少;"在标识符ItemType"之前
错误 C4430:缺少类型说明符 - 假定为 int.注意:C++ 不支持 default-int
但我不明白为什么当它是 char
时它可以正常工作.
But I don't understand why it works correctly when it is a char
.
我知道这是很多代码,但 typedef 在第 7 行.我包含了整个内容,因为我不知道会发生什么.
I know this is a lot of code but the typedef is on the 7th line. I included the whole thing because I don't know what to expect.
#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();
}
推荐答案
名称 EventInfo
仅从其声明点开始可用.你的 typedef 太早了,编译器还不知道 EventInfo
是什么.
The name EventInfo
is only usable from its point of declaration forward. Your typedef is too early, and the compiler doesn't yet know what a EventInfo
is.
您可以将 typedef 移到类定义下方,也可以告诉编译器 EventInfo
是一个类:
You can move the typedef below the class definition, or you can tell the compiler that EventInfo
is a class:
typedef class EventInfo ItemType;
这篇关于为什么这个 typedef 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!