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

3

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

0

Try using

g++ main.cpp implementaion.cpp

Probably this will help

Comments

0

You can create a run file with:

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

and run it with :

./main

1 Comment

Looks like you forgot to finish your thought here? I'm confused by this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.