0

Hi i read the other questions and answers about undefined reference.But still i'm not able to find out what are the problems with my code. I have a simple linked list code wherein i add the integers to the tail and after that i display them. Here is my code "head.h"

#ifndef __HEAD_H_INCLUDE
#define __HEAD_H_INCLUDE

class Node {
int info;
Node *next;
};

class imple {
public:
    imple();
    void addToTail(int );
    void display(void);
private:
    Node *head,*tail;
};

#endif

"implementaion.cpp"

#include<iostream>
#include "head.h"

imple::imple(){
head=tail=0;
}

void imple::addToTail(int key){
if(tail==0)
    {tail=head=new Node();
    info=key;next=0;}
else
{
    tail->next=new Node();
    info=key;next=0;
    tail=tail->next;
}
}

void imple::display(){
Node *temp;
for(temp=head;temp->next !=0;temp=temp->next)
{
    std::cout<<temp->info << "   ";
}
}

"main.cpp"

#include<iostream>
#include "head.h"

int main(){
Node node;
imple ab;
for(int i=0;i<5;i++)
ab.addToTail(i);
ab.display();
}

Everytime i compile i get this error

"/tmp/cc20Z1ZH.o: In function main': lmain.cpp:(.text+0x10): undefined reference toimple::imple()' lmain.cpp:(.text+0x2a): undefined reference to imple::addToTail(int)' lmain.cpp:(.text+0x45): undefined reference toimple::display()' collect2: ld returned 1 exit status"

Your answers and suggestions will be helpful

Hemanth
  • 1
  • 1
  • 1
  • 4
  • How do you compile? What is the compilation command line? – Ferenc Deak Oct 20 '15 at 07:36
  • 3
    What does your command line look like ? Are you actually compiling `implementaion.cpp` ? – Paul R Oct 20 '15 at 07:37
  • 2
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Niall Oct 20 '15 at 07:44

3 Answers3

3

In short, you may use

g++ main.cpp implementation.cpp -o out

You need to include implementation.cppin your building process and make the function definitions accessible to the linker. That is, compile it with

g++ -c implementation.cpp -o implementation.o

and

g++ -c main.cpp -o main.o

and link them together with

g++ main.o implementation.o -o out
cadaniluk
  • 15,027
  • 2
  • 39
  • 67
0

Try using

g++ main.cpp implementaion.cpp

Probably this will help

alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51
0

You can create a run file with:

g++ -o main implementation.cpp main.cpp

and run it with :

./main