我正在尝试用C ++实现我自己的Doubly Linked List(是的,我知道std已经有了一个)并且我不断得到着名的邪恶编译器错误:
ld: symbol(s) not found for architecture x86_64
我知道这很常见,而且我已经多次发生并解决了它,但是我似乎无法弄清楚为什么这次会发生这种情况。我已经看到了关于该主题的几乎所有SO线程,并且它们似乎都没有纠正它。
我正在使用类模板,但是我的所有成员函数都在头文件中内联,并且实现了所有声明的函数。我在Retina Macbook pro上使用最新的Eclipse,用clang编译。
请帮助!!!
/*
* StackDLL.h
*/
#ifndef STACKDLL_H_
#define STACKDLL_H_
#include <iostream>
namespace std {
template <typename T>
class StackDLL {
/* Node in the DLL */
struct Node {
T data; // data held in node
Node* next;
Node* prev;
Node(T data);
Node(T data, Node* n, Node* p); // constructor
T getData(); // gets data
Node* getNext(); // gets node's next member
Node* getPrev(); // gets previous member
void setData(T data);
}; // struct Node
private:
Node* head; // pointer to head of DLL
Node* tail; // pointer to tail of DLL
int size; // size of list
public:
StackDLL() {
this->head = NULL;
this->tail = NULL;
this->size = 0;;
}
~StackDLL() {
while (head) {
Node* temp(head);
head=head->next;
delete temp;
}
}
void add(T data); // adds new node to DLL holding data of type T
void remove(); // removes current element from list
bool empty() const;
operator bool() const { return !empty(); } // checks if empty
};
template<typename T>
StackDLL<T>::Node::Node(T data)
{
this->data = data;
this->next = this->prev = NULL; // set next and previous pointers to NULL
}
template<typename T>
StackDLL<T>::Node::Node(T data, Node* n, Node* p)
{
this->data = data;
this->next = n;
this->prev = p;
}
/*
* Returns Data at node
*/
template<typename T>
T StackDLL<T>::Node::getData()
{
return this->data;
}
/*
* returns next pointer of a given node
*/
template<typename T>
typename StackDLL<T>::Node* StackDLL<T>::Node::getNext()
{
return this->next;
}
/*
* returns previous pointer of a given node
*/
template<typename T>
typename StackDLL<T>::Node* StackDLL<T>::Node::getPrev()
{
return this->prev;
}
/*
* Sets data of a node
*/
template<typename T>
void StackDLL<T>::Node::setData(T data)
{
this->data = data;
}
/*
* adds a new node holding data to the DLL
*/
template<typename T>
void StackDLL<T>::add(T data)
{
// create new node and make it tail
Node* node = new Node(data);
// If list is empty, both head/tail point to new element
if ( this->empty() ) {
this->head = node;
this->tail = node;
node->prev = NULL; // update previous to null
}
else {
node->prev = tail; // if not empty, previous = tail
}
this->tail = node; // update DLL tail pointer
this->size++; // increment size
}
/*
* removes node from DLL
*/
template<typename T>
void StackDLL<T>::remove()
{
if (! empty() ) {
tail = tail->prev; // previous element becomes new tail
tail->next = NULL;
}
}
/*
* checks if DLL is empty
*/
template<typename T>
bool StackDLL<T>::empty() const
{
return ( !head || !tail );
}
} // namespace std
#endif /* STACKDLL_H_ */
这是实际的编译器错误:
21:12:17 **** Incremental Build of configuration Debug for project project5 ****
make all
Building target: project5
Invoking: MacOS X C++ Linker
g++ -o "project5" ./DLLdriver.o ./StackDLL.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [project5] Error 1
21:12:17 Build Finished (took 61ms)