怎样用C++中的new改写C中的malloc
来源:百度知道 编辑:UC知道 时间:2024/06/26 03:11:26
我在编写队列程序的时候,想将malloc改成new,但出错了。为什么呢?
Q.front=(QueuePtr)malloc(sizeof(QNode));//编译成功!!!
QueuePtr Q.front=new QNode;//失败!!!
程序如下:(只要编译成功就行,不用连接了)
#include <iostream>
using namespace std;
const int OVERFLOW=-2;
const int OK=1;
typedef char QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;//对头指针
QueuePtr rear;//队尾指针
}LinkQueue;
//构造一个空队列
int InitQueue(LinkQueue &Q){
QueuePtr Q.front=new QNode;//问题所在
if(!Q.front) exit(OVERFLOW);
Q.rear=Q.front;
Q.front->next=NULL;
return OK;
}//InitQueue
请求大虾们多多指教,不胜感激!
Q.front=(QueuePtr)malloc(sizeof(QNode));//编译成功!!!
QueuePtr Q.front=new QNode;//失败!!!
程序如下:(只要编译成功就行,不用连接了)
#include <iostream>
using namespace std;
const int OVERFLOW=-2;
const int OK=1;
typedef char QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;//对头指针
QueuePtr rear;//队尾指针
}LinkQueue;
//构造一个空队列
int InitQueue(LinkQueue &Q){
QueuePtr Q.front=new QNode;//问题所在
if(!Q.front) exit(OVERFLOW);
Q.rear=Q.front;
Q.front->next=NULL;
return OK;
}//InitQueue
请求大虾们多多指教,不胜感激!
QueuePtr Q.front=new QNode;//问题所在
改为
Q.front=new QNode;
#include <iostream>
using namespace std;
const int OVERFLOW=-2;
const int OK=1;
typedef char QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;//对头指针
QueuePtr rear;//队尾指针
}LinkQueue;
//构造一个空队列
int InitQueue(LinkQueue &Q){
Q.front=new QNode;///////////////////////如此修改
if(!Q.front) exit(OVERFLOW);
Q.rear=Q.front;
Q.front->next=NULL;
return OK;
}//InitQueue
QueuePtr Q.front=new QNode;
去掉最前面的类型定义就可以了.
Q.front=new QNode;
QueuePtr * Q.front=new QNode,new返回的是地址,试试看,我没有Vc++