0

很长一段时间后,我才重新开始使用 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,现在却面临着奇怪的问题,而不是专注于编码。

单链表.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_ */

主文件

 #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;
}
4

2 回答 2

2

实际上,这不起作用的原因是因为您没有为模板类定义头文件,具有单独的实现,因为模板没有编译器生成的实现。因此,拥有 .cpp 文件是不正确的 c++。将其全部内联或仅保留在标题中。请参阅最底部的以下内容

于 2012-04-29T19:46:06.053 回答
0

您需要确保它们位于构建路径中,以便 eclipse 知道将 .cpp 文件链接到二进制文件的其余部分。转到项目-> 属性-> c++ 常规-> 路径和符号-> 源位置,您应该可以从那里设置它。再次构建时,通过查看构建日志确保正在构建包含实现的 .cpp 文件。

如果您不包括整个构建日志,那么添加您的源代码并没有太大帮助,只是说。

于 2012-04-29T17:50:08.080 回答