为什么提示“error C2065: 'malloc' : undeclared identifier”

zhang11wu4 2007-09-18 05:51:00
指示的出错语句是:
s->base=(ElemType *)malloc(StackInitSize*sizeof(ElemType));
错误提示:
--------------------Configuration: chp34 - Win32 Debug--------------------
Compiling...
convert.cpp
d:\数据结构算法\chp34\dy_hanshu.cpp(8) : error C2065: 'malloc' : undeclared identifier
Error executing cl.exe.

convert.obj - 1 error(s), 0 warning(s)
这是为什么呀?
...全文
9414 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qt20120 2012-08-12
  • 打赏
  • 举报
回复
谢谢!
Ben_jli 2012-08-09
  • 打赏
  • 举报
回复
应该是包含crtdbg.h
Tiger_Zhao 2007-09-20
  • 打赏
  • 举报
回复
非算法问题请到相应讨论区 http://community.csdn.net/Expert/ForumsList.asp?typenum=1&roomid=56
mmmcd 2007-09-20
  • 打赏
  • 举报
回复

typedef int ElemType;
放到头文件c1.h里面
zhang11wu4 2007-09-18
  • 打赏
  • 举报
回复
我按楼上的改过来了,能编译了,但link时却出现如下错误,这是为什么:
--------------------Configuration: chp34 - Win32 Debug--------------------
Compiling...
Dy_hanshu.cpp
d:\数据结构算法\chp34\c1.h(7) : error C2143: syntax error : missing ';' before '*'
d:\数据结构算法\chp34\c1.h(7) : error C2501: 'ElemType' : missing storage-class or type specifiers
d:\数据结构算法\chp34\c1.h(7) : error C2501: 'base' : missing storage-class or type specifiers
d:\数据结构算法\chp34\c1.h(8) : error C2143: syntax error : missing ';' before '*'
d:\数据结构算法\chp34\c1.h(8) : error C2501: 'ElemType' : missing storage-class or type specifiers
d:\数据结构算法\chp34\c1.h(8) : error C2501: 'top' : missing storage-class or type specifiers
D:\数据结构算法\chp34\Dy_hanshu.cpp(9) : error C2039: 'base' : is not a member of 'Dy_SqStack'
d:\数据结构算法\chp34\c1.h(6) : see declaration of 'Dy_SqStack'
D:\数据结构算法\chp34\Dy_hanshu.cpp(9) : error C2065: 'ElemType' : undeclared identifier
D:\数据结构算法\chp34\Dy_hanshu.cpp(9) : error C2059: syntax error : ')'
D:\数据结构算法\chp34\Dy_hanshu.cpp(10) : error C2039: 'base' : is not a member of 'Dy_SqStack'
d:\数据结构算法\chp34\c1.h(6) : see declaration of 'Dy_SqStack'
D:\数据结构算法\chp34\Dy_hanshu.cpp(15) : error C2039: 'top' : is not a member of 'Dy_SqStack'
d:\数据结构算法\chp34\c1.h(6) : see declaration of 'Dy_SqStack'
D:\数据结构算法\chp34\Dy_hanshu.cpp(15) : error C2039: 'base' : is not a member of 'Dy_SqStack'
d:\数据结构算法\chp34\c1.h(6) : see declaration of 'Dy_SqStack'
D:\数据结构算法\chp34\Dy_hanshu.cpp(18) : error C2061: syntax error : identifier 'ElemType'
D:\数据结构算法\chp34\Dy_hanshu.cpp(20) : error C2039: 'top' : is not a member of 'Dy_SqStack'
d:\数据结构算法\chp34\c1.h(6) : see declaration of 'Dy_SqStack'
D:\数据结构算法\chp34\Dy_hanshu.cpp(20) : error C2039: 'base' : is not a member of 'Dy_SqStack'
d:\数据结构算法\chp34\c1.h(6) : see declaration of 'Dy_SqStack'
D:\数据结构算法\chp34\Dy_hanshu.cpp(22) : error C2039: 'base' : is not a member of 'Dy_SqStack'
d:\数据结构算法\chp34\c1.h(6) : see declaration of 'Dy_SqStack'
D:\数据结构算法\chp34\Dy_hanshu.cpp(22) : error C2059: syntax error : ')'
D:\数据结构算法\chp34\Dy_hanshu.cpp(24) : error C2039: 'top' : is not a member of 'Dy_SqStack'
d:\数据结构算法\chp34\c1.h(6) : see declaration of 'Dy_SqStack'
D:\数据结构算法\chp34\Dy_hanshu.cpp(24) : error C2065: 'x' : undeclared identifier
D:\数据结构算法\chp34\Dy_hanshu.cpp(26) : error C2039: 'top' : is not a member of 'Dy_SqStack'
d:\数据结构算法\chp34\c1.h(6) : see declaration of 'Dy_SqStack'
D:\数据结构算法\chp34\Dy_hanshu.cpp(28) : error C2146: syntax error : missing ';' before identifier 'pop'
D:\数据结构算法\chp34\Dy_hanshu.cpp(28) : error C2501: 'ElemType' : missing storage-class or type specifiers
D:\数据结构算法\chp34\Dy_hanshu.cpp(28) : fatal error C1004: unexpected end of file found
Error executing cl.exe.



主程序:convert.cpp
typedef int ElemType;
#include "Dy_hanshu.cpp"

void convert()
{
ElemType elem;
Dy_SqStack s;
StackInit(&s);
for(int i=1;i<6;i++)
{
push(&s,i);
}
while(!EmptyStack(&s))
{
elem=pop(&s);
printf("%d",elem);
}
}
void main()
{
convert();
}

头文件c1.h:
#define StackInitSize 100
#define StackIncrement 10
#define TRUE 1
#define FALSE 0
typedef struct
{
ElemType *base;
ElemType *top;
int maxsize;
}Dy_SqStack;


程序Dy_hanshu.cpp
#include "c1.h"
#include "stdio.h"
#include "stdlib.h"



void StackInit(Dy_SqStack *s)
{
s->base=(ElemType *)malloc(StackInitSize*sizeof(ElemType));
if(!s->base)
{
printf("OVERFLOW"); //分配空间失败
return;
}
s->top=s->base;
s->maxsize = StackInitSize;
}
void push(Dy_SqStack *s,ElemType x)
{
if((s->top - s->base) == s->maxsize)
{
s->base =(ElemType *)realloc(s->base,(s->maxsize+StackIncrement)*sizeof(ElemType));
}
*(s->top) = x;
s->maxsize = s->maxsize + StackIncrement;
s->top ++;
}
ElemType pop(Dy_SqStack *s)
{
if(s->top == s->base)
{
printf("UNDERFLOW");
}
s->top --;
return (*(s->top));

}
int EmptyStack(Dy_SqStack *s)
{
if(s->top == s->base)
return TRUE;
return FALSE;
}



mathe 2007-09-18
  • 打赏
  • 举报
回复
没有包含申明malloc的标准头文件。
可能使malloc.h或stdlib.h等

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧