Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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))        // Undefined referrence error
    {
        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);    // Undefined referrence error
    pop(ss);    // Undefined referrence error
    pop(ss);    // Undefined referrence error
    pop(ss);    // Undefined referrence error
    pop(ss);    // Undefined referrence error
    pop(ss);    // Undefined referrence error
   int c= peek(ss);
   printf("%d",c);
}
Posted
Updated 6-May-21 19:53pm
v2
Comments
[no name] 7-May-21 1:17am    
Have you included all the library files ? If not, try including malloc and stdlib
_-_-_-me 7-May-21 1:29am    
Oh,Thank you . I forgot to include stdlib, but even after including , i am getting error .i.e (undefined referrence to the function).

There are a couple of problems. You inserted the definitions of isEmpty() and pop() before the closing brace of push(). Add a } before the defintion of isEmpty() and delete one of the two at the end of pop() to fix that.

Then, you call malloc() but haven't included <stdlib.h>. That may work on your compiler but not mine.

Also, you define a function named pop() but prototyped it at the top as pull(). That won't affect anything yet, but if you later code pull() instead of pop() then you'll get an error.

Those changes will get you past debugging your typing and into testing your logic. :^)
 
Share this answer
 
v2
C++
#include<stdio.h>
#include<stdlib.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 pop(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 1;
    }
}
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);
    int d= peek(ss);
   if(d==0)
   printf("Stack is empty");
   if(d==1)
   printf("%d\n",ss->a[ss->top]);
    pop(ss);
    pop(ss);
    pop(ss);
    pop(ss);
    pop(ss);
    pop(ss);
   int c= peek(ss);
   if(c==0)
   printf("Stack is empty");
   if(c==1)
   printf("%d\n",ss->a[ss->top]);
   return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900