Reputation: 1
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 to
imple::imple()'
lmain.cpp:(.text+0x2a): undefined reference to imple::addToTail(int)'
lmain.cpp:(.text+0x45): undefined reference to
imple::display()'
collect2: ld returned 1 exit status"
Your answers and suggestions will be helpful
Upvotes: 0
Views: 11746
Reputation: 52
You can create a run file with:
g++ -o main implementation.cpp main.cpp
and run it with :
./main
Upvotes: 0
Reputation: 15229
In short, you may use
g++ main.cpp implementation.cpp -o out
You need to include implementation.cpp
in 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
Upvotes: 3
Reputation: 6440
Try using
g++ main.cpp implementaion.cpp
Probably this will help
Upvotes: 0