The programme is about implementations on stack (abstract data type).
Error:
/tmp/ccHOoQeL.o: In function `peek':
main.c:(.text+0x102): undefined reference to `isEmpty'
/tmp/ccHOoQeL.o: In function `main':
main.c:(.text+0x1b5): undefined reference to `pop'
main.c:(.text+0x1c6): undefined reference to `pop'
main.c:(.text+0x1d7): undefined reference to `pop'
main.c:(.text+0x1e8): undefined reference to `pop'
main.c:(.text+0x1f9): undefined reference to `pop'
/tmp/ccHOoQeL.o:main.c:(.text+0x20a): more undefined references to `pop' follow
collect2: error: ld returned 1 exit status
Error in normal words:
Undefined reference to isEmpty() function in peek() function and undefined reference
to pop() function which is in main function.
Undefined referrence:
An “Undefined Reference” error’s occurs when we have a reference to an object name
(class, function, variable, and so on) in our program, and the linker cannot find
its definitions when it tries to search for it in all the related item files and
libraries.
But here the funtions peek() and isEmpty() are clearly defined and declared in the program . Then,why the linker could not find the function definitions? so, Why we are getting this error?
Is there any mistake in the definitions of the function and declaration of the functions in the given programme so that the linker could not find the function definitions?
What I have tried:
#include<stdio.h>
struct stack {
int top;
int capacity;
int *a;
};
struct stack* createthestack(int);
int isFull(struct stack*);
void push(struct stack*,int);
int isEmpty(struct stack*);
void pull(struct stack*);
int peek(struct stack*);
struct stack* createthestack(int capacity)
{
struct stack* Stack=(struct stack*)malloc(sizeof(struct stack));
Stack->top=-1;
Stack->capacity=capacity;
Stack->a=(int*)malloc(sizeof(int)*capacity);
return Stack;
}
int isFull(struct stack* ss)
{
if((ss->top)==(ss->capacity)-1)
{
return 1;
}
else
{
return 0;
}
}
void push(struct stack* ss, int x)
{
if(isFull(ss))
{
printf("Stack is full\n");
}
else
{
ss->top=ss->top-1;
ss->a[ss->top]=x;
printf("%d element is pushed\n",x);
}
int isEmpty(struct stack* ss)
{
if((ss->top)==-1)
{
return 1;
}
else
{
return 0;
}
}
void pop(struct stack* ss)
{
if(isEmpty(ss))
{
printf("Stack is empty\n");
}
else
{
printf("%d is popped\n",ss->a[ss->top]);
(ss->top)=(ss->top)-1;
}
}
}
int peek(struct stack* ss)
{
if(isEmpty(ss)) {
return 0;
}
else
{
return ss->a[ss->top];
}
}
int main()
{
struct stack* ss=createthestack(5);
push(ss,5);
push(ss,4);
push(ss,3);
push(ss,2);
push(ss,1);
push(ss,0);
pop(ss); pop(ss); pop(ss); pop(ss); pop(ss); pop(ss); int c= peek(ss);
printf("%d",c);
}