typedef的用法,散分50
///Queue.h
///
///file 1
///
#ifndef QUEUE_H
#define QUEUE_H
#include<iostream.h>
//*************************************************************
编译就这句出现问题
typedef QueueEntry NodeEntry;
//**************************************************************//
enum ErrorCode {success,underflow,overflow};
struct Node{
Node *next;
NodeEntry entry;
Node();
Node(const NodeEntry &item,Node *heap=NULL);
};
class Queue{
public:
Queue();
~Queue();
ErrorCode append(const QueueEntry &item);
ErrorCode serve();
ErrorCode retrieve(QueueEntry &item)const;
void print()const;
void clear();
int size() const;
protected:
Node *tail;
};
#endif
//**************************************************************
//queue.cpp
//file 2
**************************************************************//
#include"queue.h"
Node::Node(){
next=NULL;
}
Node::Node(const QueueEntry &item,Node *heap){
next=heap;
entry=item;
}
Queue::Queue(){
tail=NULL;
}
Queue::~Queue(){
while(serve()==success);
}
ErrorCode Queue::append(const QueueEntry &item){
Node *NewTail=new Node(item);
if(NewTail==NULL)
return overflow;
if(tail==NULL){
tail=NewTail;
tail->next=tail;
}
else{
NewTail->next=tail->next;
tail->next=NewTail;
tail=NewTail;
}
return success;
}
ErrorCode Queue::serve(){
if(tail==NULL)
return underflow;
Node *front=tail->next;
tail->next=front->next;
if(front==front->next)
tail=NULL;
else
delete front;
return success;
}
ErrorCode Queue::retrieve(QueueEntry &item)const{
if(tail==NULL)
return underflow;
Node *front=tail->next;
item=front->entry;
return success;
}
void Queue::print()const{
if(tail){
Node *front=tail->next;
if(tail)
do{
cout<<"The number: "<<front->entry<<endl;
front=front->next;
}while(front!=tail->next);
}
}
void Queue::clear(){
if(tail){
Node *front=tail->next;
Node *AnotherFront=front->next;
do{
delete front;
front=AnotherFront;
AnotherFront=AnotherFront->next;
}while(AnotherFront!=tail);
delete tail;
tail=NULL;
}
}
int Queue::size()const{
int i=0;
if(tail){
Node *front=tail->next;
while(front!=tail){
i++;
front=front->next;}
}
else
return 0;
return i+1;
}
//****************************************************************
//main.cpp
//file 3
//**************************************************************//
#include<iostream.h>
typedef int QueueEntry;
#include"queue.h"
inline void help(){
cout<<"The imformation to help you use the program\n\n"
<<"H-to get help\n"
<<"A-append a number to team\n"
<<"S-serve,delete a number on the top\n"
<<"R-retrieve a number to a symbol\n"
<<"L-return the length of the entry\n"
<<"C-clear the team\n"
<<"D-display the team with all eletems\n"
<<"Q-exit the program\n"
<<"Would you like to have a try now?\n"
<<endl;
}
char GetCommand(){
char n;
cin>>n;
while(!(n=='H'||n=='A'||n=='S'||n=='R'||n=='r'||n=='C'||n=='D'||n=='Q'||n=='L')){
help();
cin>>n;
}
return n;
}
bool DoCommand(char n,Queue &q){
QueueEntry symbol;
switch(n){
case 'H':
help();
break;
case 'A':
cout<<"Please input: ";
cin>>symbol;
q.append(symbol);
break;
case 'S':
q.serve();
break;
case 'R':
if(q.retrieve(symbol)!=underflow)
cout<<"The top is: "<<symbol<<endl;
break;
case 'C':
cout<<"clearing...\n "<<endl;
q.clear();
break;
case 'D':
q.print();
break;
case 'L':
cout<<"the size of it is: "<<q.size()<<endl;
break;
case 'Q':
return false;
}
return true;
}
void main(){
help();
Queue q;
while(DoCommand(GetCommand(),q));
}
问题点数:50、回复次数:25Top
1 楼fireflyc(萤火虫)回复于 2005-04-23 00:16:12 得分 2
在typedef QueueEntry NodeEntry;前没有QueueEntryTop
2 楼ybb1567()回复于 2005-04-23 00:30:04 得分 0
麻烦把修改后可执行的发上来,谢谢!Top
3 楼ybb1567()回复于 2005-04-23 00:30:33 得分 0
//****************************************************************
//main.cpp
//file 3
//**************************************************************//
#include<iostream.h>
typedef int QueueEntry;
#include"queue.h"
Top
4 楼xjp6688(大平/要做必须最好)回复于 2005-04-23 08:39:24 得分 8
//---------------------------------------------------------------------------
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
#include <iostream>
#include <stdlib.h>
#ifndef QUEUE_H
#define QUEUE_H
#include<iostream.h>
using namespace std;
typedef int QueueEntry;
typedef QueueEntry NodeEntry;
enum ErrorCode {success,underflow,overflow};
struct Node{
Node *next;
NodeEntry entry;
Node();
Node(const NodeEntry &item,Node *heap=NULL);
};
class Queue{
public:
Queue();
~Queue();
ErrorCode append(const QueueEntry &item);
ErrorCode serve();
ErrorCode retrieve(QueueEntry &item)const;
void print()const;
void clear();
int size() const;
protected:
Node *tail;
};
#endif
Node::Node(){
next=NULL;
}
Node::Node(const QueueEntry &item,Node *heap){
next=heap;
entry=item;
}
Queue::Queue(){
tail=NULL;
}
Queue::~Queue(){
while(serve()==success);
}
ErrorCode Queue::append(const QueueEntry &item){
Node *NewTail=new Node(item);
if(NewTail==NULL)
return overflow;
if(tail==NULL){
tail=NewTail;
tail->next=tail;
}
else{
NewTail->next=tail->next;
tail->next=NewTail;
tail=NewTail;
}
return success;
}
ErrorCode Queue::serve(){
if(tail==NULL)
return underflow;
Node *front=tail->next;
tail->next=front->next;
if(front==front->next)
tail=NULL;
else
delete front;
return success;
}
ErrorCode Queue::retrieve(QueueEntry &item)const{
if(tail==NULL)
return underflow;
Node *front=tail->next;
item=front->entry;
return success;
}
void Queue::print()const{
if(tail){
Node *front=tail->next;
if(tail)
do{
cout<<"The number: "<<front->entry<<endl;
front=front->next;
}while(front!=tail->next);
}
}
void Queue::clear(){
if(tail){
Node *front=tail->next;
Node *AnotherFront=front->next;
do{
delete front;
front=AnotherFront;
AnotherFront=AnotherFront->next;
}while(AnotherFront!=tail);
delete tail;
tail=NULL;
}
}
int Queue::size()const{
int i=0;
if(tail){
Node *front=tail->next;
while(front!=tail){
i++;
front=front->next;}
}
else
return 0;
return i+1;
}
typedef int QueueEntry;
inline void help(){
cout<<"The imformation to help you use the program\n\n"
<<"H-to get help\n"
<<"A-append a number to team\n"
<<"S-serve,delete a number on the top\n"
<<"R-retrieve a number to a symbol\n"
<<"L-return the length of the entry\n"
<<"C-clear the team\n"
<<"D-display the team with all eletems\n"
<<"Q-exit the program\n"
<<"Would you like to have a try now?\n"
<<endl;
}
char GetCommand(){
char n;
cin>>n;
while(!(n=='H'||n=='A'||n=='S'||n=='R'||n=='r'||n=='C'||n=='D'||n=='Q'||n=='L')){
help();
cin>>n;
}
return n;
}
bool DoCommand(char n,Queue &q){
QueueEntry symbol;
switch(n){
case 'H':
help();
break;
case 'A':
cout<<"Please input: ";
cin>>symbol;
q.append(symbol);
break;
case 'S':
q.serve();
break;
case 'R':
if(q.retrieve(symbol)!=underflow)
cout<<"The top is: "<<symbol<<endl;
break;
case 'C':
cout<<"clearing...\n "<<endl;
q.clear();
break;
case 'D':
q.print();
break;
case 'L':
cout<<"the size of it is: "<<q.size()<<endl;
break;
case 'Q':
return false;
}
return true;
}
void main(){
help();
Queue q;
while(DoCommand(GetCommand(),q));
}
---------------
在BCB6下调试成功
--------------------
The imformation to help you use the program
H-to get help
A-append a number to team
S-serve,delete a number on the top
R-retrieve a number to a symbol
L-return the length of the entry
C-clear the team
D-display the team with all eletems
Q-exit the program
Would you like to have a try now?
Top
5 楼zhongwei5695(威少求学C++)回复于 2005-04-23 08:40:49 得分 0
upTop
6 楼ybb1567()回复于 2005-04-23 09:16:42 得分 0
#pragma argsused
#include <iostream>
#include <stdlib.h>
#ifndef QUEUE_H
#define QUEUE_H
#include<iostream.h>
using namespace std;
typedef int QueueEntry;
//***********************************************
写上面那个程序,我目的是采用多文件的格式,而且希望在主文件中可以自由定义QueueEntry的类型,而不是在类的头文件定义。
很奇怪的是,我那个程序如果合并为一个的时候,就能够编译执行,但是分开成三个的时候,出现了以下问题:
麻烦各位再帮忙下啦~
Top
7 楼ybb1567()回复于 2005-04-23 09:17:44 得分 0
Compiling...
queue.cpp
d:\收获\c++程序设计教程_deitel\f\queue.h(4) : error C2146: syntax error : missing ';' before identifier 'NodeEntry'
d:\收获\c++程序设计教程_deitel\f\queue.h(4) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
f.exe - 2 error(s), 0 warning(s)Top
8 楼GuodongHe(国栋)回复于 2005-04-23 10:29:08 得分 2
在typedef QueueEntry NodeEntry;之前加条语句
class QueueEntry ;
这种做法比较值得推荐,符合C++标准,在任何编译器上都没有问题
Top
9 楼ericqxg007(还有很多东西要学(卡卡一米阳光))回复于 2005-04-23 13:18:24 得分 0
我不知道 自己在调程序时也用到了这个同样是报错
帮你一起找答案
还有问问楼上的加 class QueueEntry 是为什么
QueueEntry不是一个类啊 如果用模板的话也不是这样的啊
请楼上的指教 谢谢Top
10 楼dongpy(51-->ARM)回复于 2005-04-23 13:36:07 得分 8
///Queue.h
///
///file 1
///
#ifndef QUEUE_H
#define QUEUE_H
#include<iostream.h>
//*************************************************************
//编译就这句出现问题
typedef int QueueEntry; ///////////////////////////////////////
typedef QueueEntry NodeEntry;
//**************************************************************//
enum ErrorCode {success,underflow,overflow};
struct Node{
Node *next;
NodeEntry entry;
Node();
Node(const NodeEntry &item,Node *heap=NULL);
};
class Queue{
public:
Queue();
~Queue();
ErrorCode append(const QueueEntry &item);
ErrorCode serve();
ErrorCode retrieve(QueueEntry &item)const;
void print()const;
void clear();
int size() const;
protected:
Node *tail;
};
#endif
//**************************************************************
//queue.cpp
//file 2
/**************************************************************/
#include"queue.h"
Node::Node(){
next=NULL;
}
Node::Node(const QueueEntry &item,Node *heap){
next=heap;
entry=item;
}
Queue::Queue(){
tail=NULL;
}
Queue::~Queue(){
while(serve()==success);
}
ErrorCode Queue::append(const QueueEntry &item){
Node *NewTail=new Node(item);
if(NewTail==NULL)
return overflow;
if(tail==NULL){
tail=NewTail;
tail->next=tail;
}
else{
NewTail->next=tail->next;
tail->next=NewTail;
tail=NewTail;
}
return success;
}
ErrorCode Queue::serve(){
if(tail==NULL)
return underflow;
Node *front=tail->next;
tail->next=front->next;
if(front==front->next)
tail=NULL;
else
delete front;
return success;
}
ErrorCode Queue::retrieve(QueueEntry &item)const{
if(tail==NULL)
return underflow;
Node *front=tail->next;
item=front->entry;
return success;
}
void Queue::print()const{
if(tail){
Node *front=tail->next;
if(tail)
do{
cout<<"The number: "<<front->entry<<endl;
front=front->next;
}while(front!=tail->next);
}
}
void Queue::clear(){
if(tail){
Node *front=tail->next;
Node *AnotherFront=front->next;
do{
delete front;
front=AnotherFront;
AnotherFront=AnotherFront->next;
}while(AnotherFront!=tail);
delete tail;
tail=NULL;
}
}
int Queue::size()const{
int i=0;
if(tail){
Node *front=tail->next;
while(front!=tail){
i++;
front=front->next;}
}
else
return 0;
return i+1;
}
//****************************************************************
//main.cpp
//file 3
//**************************************************************//
#include<iostream.h>
#include"queue.h"
inline void help(){
cout<<"The imformation to help you use the program\n\n"
<<"H-to get help\n"
<<"A-append a number to team\n"
<<"S-serve,delete a number on the top\n"
<<"R-retrieve a number to a symbol\n"
<<"L-return the length of the entry\n"
<<"C-clear the team\n"
<<"D-display the team with all eletems\n"
<<"Q-exit the program\n"
<<"Would you like to have a try now?\n"
<<endl;
}
char GetCommand(){
char n;
cin>>n;
while(!(n=='H'||n=='A'||n=='S'||n=='R'||n=='r'||n=='C'||n=='D'||n=='Q'||n=='L')){
help();
cin>>n;
}
return n;
}
bool DoCommand(char n,Queue &q){
QueueEntry symbol;
switch(n){
case 'H':
help();
break;
case 'A':
cout<<"Please input: ";
cin>>symbol;
q.append(symbol);
break;
case 'S':
q.serve();
break;
case 'R':
if(q.retrieve(symbol)!=underflow)
cout<<"The top is: "<<symbol<<endl;
break;
case 'C':
cout<<"clearing...\n "<<endl;
q.clear();
break;
case 'D':
q.print();
break;
case 'L':
cout<<"the size of it is: "<<q.size()<<endl;
break;
case 'Q':
return false;
}
return true;
}
void main(){
help();
Queue q;
while(DoCommand(GetCommand(),q));
}
Top
11 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2005-04-23 13:37:13 得分 2
类前置声名一下Top
12 楼ybb1567()回复于 2005-04-23 14:18:50 得分 0
//*************************************************************************************//
在typedef QueueEntry NodeEntry;之前加条语句
class QueueEntry ;
这种做法比较值得推荐,符合C++标准,在任何编译器上都没有问题
//*************************************************************************************//
试过,没有用,错误变成八个。
class QueueEntry;这种语法从没有见过,不知道为什么说可以这样?
麻烦解释下Top
13 楼ybb1567()回复于 2005-04-23 14:22:54 得分 0
回复人: dongpy(51-->ARM) ( ) 信誉:116 2005-04-23 13:36:00 得分: 0
//*********************************************************************//
//编译就这句出现问题
typedef int QueueEntry;
typedef QueueEntry NodeEntry;
//*********************************************************************//
我知道这样做可以,但是我希望定义QueueEntry的类型是放在主程序中,而不是放在头文件里,
就是希望用户可以自由定义一个类型,而不是要通过修改头文件代码:
typedef float(int,double...) QueueEntry;
主要从封装的角度考虑的。
麻烦再说说。
Top
14 楼ybb1567()回复于 2005-04-23 14:24:29 得分 0
回复人: oyljerry(【勇敢的心】→ ㊣Contributing A za!㊣) ( ) 信誉:100 2005-04-23 13:37:00 得分: 0
类前置声名一下
//***********************************************************************************
请问类前置怎么声明法?
不甚感激!
Top
15 楼useresu(俗人)(灌水是我无言的抗议)回复于 2005-04-23 14:34:17 得分 2
没有声明QueueEntry,怎么typedef啊
所以要在上边加class QueueEntry ;Top
16 楼useresu(俗人)(灌水是我无言的抗议)回复于 2005-04-23 14:40:13 得分 0
class QueueEntry ;就是类前置声明Top
17 楼ybb1567()回复于 2005-04-23 14:51:31 得分 0
#ifndef QUEUE_H
#define QUEUE_H
#include<iostream.h>
//*************************************************************
class QueueEntry ;
typedef QueueEntry NodeEntry;
//**************************************************************//
这样声明么?但我调试不行。为什么可以通过class QueueEntry;声明?我理解这不是对一个类的声明么?
谢谢!
Top
18 楼GuodongHe(国栋)回复于 2005-04-23 15:04:01 得分 0
我没有细看你的QueueEntry到底是个什么,如果是你自己定义的一个类,就在前面加一个
class QueueEntry;如果是一个内置的类型,就用一个typedef int(or char...) QueueEntryTop
19 楼ybb1567()回复于 2005-04-23 15:18:06 得分 0
QueueEntry是个内置类型,这个程序是循环链表实现队列的。
//就用一个typedef int(or char...) QueueEntry
我明白您的意思,就是在类定义文件之中声明为:QueueEntry 是int类型。
但我希望不是在头文件就给QueueEntry定义类型,而是在main文件中定义它。
先感谢各位了.Top
20 楼ybb1567()回复于 2005-04-23 17:37:39 得分 0
我写很多程序都遇见这个问题,各位帮帮忙嘛~
顶一下。。。。Top
21 楼ybb1567()回复于 2005-04-23 19:05:38 得分 0
CSDN这么大,居然连一个语法错误都没有人知道么????????????????????????????????????????????????????????????????????????????????????????????????????????????Top
22 楼arrowcy(长弓手)回复于 2005-04-23 19:51:15 得分 0
知道楼主的想在main里面来定义那个类型,这样就可以做到用户用的时候不用修改那个队列的cpp及h文件,但这样好像不容易做到吧,除非用函数模板或者类模板,这个才是专门用来达到这种目的的Top
23 楼arrowcy(长弓手)回复于 2005-04-23 19:54:54 得分 0
CSDN这么大,居然连一个语法错误都没有人知道么?
===============================================================
我觉得csdn里面人人都知道这个错误,也有办法解决,只不过你的要求也太那个什么了,本来就是用的C++,这个时候用模板多好的,何必非要用C语言那个时代的功能不怎么样的typedef呢Top
24 楼arrowcy(长弓手)回复于 2005-04-23 20:03:44 得分 26
A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the type-declaration portion of the declaration.
这是从csdn里面抄下来的一段话,根据这段话可以知道,在一个文件里面用typedef定义的类型,旨在这个文件以及宝汉这个文件的文件中有效,所以,除非那个queue.cpp或者queue.h包含了一个文件,而这个文件里面定义了那个QueueEntry,否则编译肯定出错。
所以对于你那个问题,可以考虑在一个新的.h文件中定义QueueEntry,并且queue.cpp包含那个.h文件,因此那个.h文件名肯定要固定下来,以后用的时候,就在这个文件里面修改QueueEntry的定义。不过这样做的实质和直接修改queue.cpp或queue.h文件的做法是一样的,还是很麻烦,所以最好还是用模板Top
25 楼ybb1567()回复于 2005-04-23 23:55:10 得分 0
谢谢各位~Top




