Thread: undefined reference

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    undefined reference

    The problem with the code is on line 14 and says undefined reference to `push(int)

    Code:
    #include<iostream>
    using namespace std;
    void push(int n);
    int pop(int &n);
    struct elem{
           int key;
           elem *next;
           }*first=NULL,*last=NULL,*p;
    
    int main()
    {int n;
    cout<<"n=";
    while(cin>>n)
    push(n);
    while(pop(n))
    cout<<n<<"";
    }
    
    void push(int n,int key)
    {p=last;
    last=new elem;
    last->key=n;
    last->next=NULL;
    if(p!=NULL)
    p->next=last;
    if(first==NULL)
    last=first;
    }
    
    int pop(int &n)
    {if(first)
    {n=first->key;
    p=first;
    first=first->next;
    if(first==NULL)
    last=first;
    delete p;
    return 1;
    }
    else return 0;}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Your indentation is rubbish -> SourceForge.net: Indentation - cpwiki

    2. Spot the difference.
    void push(int n);
    ...
    push(n);
    ...
    void push(int n,int key)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    In other words you are trying to use a function that you have not defined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference to ...
    By DeletedUserAccount in forum C Programming
    Replies: 16
    Last Post: 01-28-2011, 02:27 PM
  2. Undefined reference ?
    By Holymanus in forum C Programming
    Replies: 6
    Last Post: 01-07-2011, 09:15 AM
  3. undefined reference to
    By diego in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2010, 03:06 PM
  4. Undefined Reference to...
    By JJFMJR in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2008, 03:09 AM
  5. Undefined Reference
    By Russell in forum C++ Programming
    Replies: 7
    Last Post: 06-02-2004, 02:08 AM