Reputation: 6969
I just resumed back to working with C++ after a long time. I am using Eclipse on Macbook Pro, and I tried just making a small project of Linked Lists.
I have been getting this error when I try compile my project:
ld: symbol(s) not found for architecture x86_64
This happens only if I #include "SingleLinkedList.h"
in my main.cpp and do operations related to SingleLinkedist
, otherwise removing the above SingleLinkedList in main, the project compiles fines. I have been looking similar questions on SO with same error, but none seem to have helped me resolve this.
Here are the class files if needed:
As far as I remember, earlier everything in the same way used to work fine on Eclipse. I just upgraded to Lion some time back, and installed the new Xcode and a new Eclispe, and now been facing weird issues instead of concentrating on coding.
SingleLinkedList.cpp
#include "SingleLinkedList.h"
template<class T>
SingleLinkedList<T>::~SingleLinkedList()
{
Node<T> *temp = head;
while(head!=0)
{
temp = head;
head = temp->next;
delete temp;
}
}
template <class T>
void SingleLinkedList<T>::addToHead(int item)
{
head = new Node<T>(item, head);
if (tail==0)
tail = head;
}
template <class T>
void SingleLinkedList<T>::addToTail(int item)
{
if(tail!=0)
{
tail->next = new Node<T>(item);
tail = tail->next;
}
else
head = tail = new Node<T>(item);
}
template <class T>
int SingleLinkedList<T>::deletefromHead()
{
int e1 = head->info;
Node<T> *temp = head;
if(head ==tail)
{
head = tail = 0;
}
else
head = temp->next;
delete temp;
return e1;
}
template <class T>
int SingleLinkedList<T>::deletefromTail()
{
int e1 = tail->info;
if(head == tail)
{
delete head;
head = tail = 0;
}
else{
Node<T> *temp = head;
while(temp->next != tail)
temp = temp->next;
delete tail;
tail = temp;
tail->next = 0;
}
return e1;
}
template <class T>
void SingleLinkedList<T>::deleteNode(int item)
{
if(head!=0) {
if(head == tail && head->info){ //If this is the only item in the linked list
delete head;
head = tail = 0;
}
else if (item == head->info){
Node<T> *temp = head;
head = temp->next;
delete temp;
}
else{
Node<T> *temp = head;
while(temp->next->info != item && temp->next !=0 ){
temp = temp->next;
}
if(temp!=0){
temp->next = temp->next->next;
temp = temp->next;
delete temp;
}
}
}
}
template <class T>
bool SingleLinkedList<T>::isEmpty()
{
return head == 0;
}
template <class T>
bool SingleLinkedList<T>::isInList(int item)const
{
Node<T> * temp = head;
while(temp!=0)
{
if(temp->info == item){
break;
}
temp = temp->next;
}
return temp->info == item;
}
template<class T>
SingleLinkedList<T>::SingleLinkedList()
{
head = tail = 0;
}
SingleLinkedList.h
#include "Node.h"
#ifndef SINGLELINKEDLIST_H_
#define SINGLELINKEDLIST_H_
template <class T>
class SingleLinkedList {
public:
SingleLinkedList();
~SingleLinkedList();
bool isEmpty();
bool isInList(int)const;
void addToHead(int);
void addToTail(int);
int deletefromHead();
int deletefromTail();
void deleteNode(int);
private:
Node<T> *head;
Node<T> *tail;
};
#endif /* SINGLELINKEDLIST_H_ */
main.cpp
#include <iostream>
#include "SingleLinkedList.h"
using namespace std;
int main() {
SingleLinkedList<int> listfile;
listfile.addToHead(2);
listfile.addToHead(4);
listfile.addToHead(6);
listfile.addToHead(8);
listfile.addToHead(10);
return 0;
}
Upvotes: 0
Views: 13894
Reputation: 1783
Actually, the reason this isn't working is because you don't define header files for template classes, with separate implementation, because templates dont have an implementation other than what the compiler generates. So it's incorrect c++ to have the .cpp file. Put it all inline or keep it in the header only. see the following at the very bottom
Upvotes: 2
Reputation: 1783
You need to make sure that they are in the build path so that eclipse knows to link the .cpp file to the rest of the binary. Go to project -> properties -> c++ general -> Paths and symbols -> source location and you should be able to set it from there. When you build again, make sure that the .cpp file that contains the implementation is being built by looking at the build log.
Adding your source doesn't really help much if you don't include the whole build log, just saying.
Upvotes: 0