久しぶりに C++ の作業を再開しました。Macbook Pro で Eclipse を使用しています。リンク リストの小さなプロジェクトを作成してみました。
プロジェクトをコンパイルしようとすると、次のエラーが発生します。
ld: symbol(s) not found for architecture x86_64
#include "SingleLinkedList.h"
これは、main.cpp で に関連する操作を行う場合にのみ発生しSingleLinkedist
ます。それ以外の場合は、main で上記の SingleLinkedList を削除すると、プロジェクトは罰金をコンパイルします。同じエラーで SO に関する同様の質問を探していましたが、これを解決するのに役立ったものはありませんでした。
必要に応じて、クラス ファイルを次に示します。
私の記憶では、以前はすべて同じ方法で Eclipse で正常に動作していました。少し前に Lion にアップグレードし、新しい Xcode と新しい Eclispe をインストールしましたが、コーディングに集中する代わりに奇妙な問題に直面しています。
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;
}