enter image description here我最近构建的代码包含以下消息:
clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)
在我构建了整个项目之后。
我尝试过:
Node.h
template<class dataType>
class Node {
private:
dataType data;
Node* next;
Node* prev;
public:
Node();
Node(dataType);
bool setNext(Node*);
bool setPrev(Node*);
Node* getNext() const;
Node* getPrev() const;
void setData(const dataType&);
dataType getData() const;
bool add(const dataType);
bool remove(const dataType);
~Node();
};
template<class dataType>
Node<dataType>::Node()
{
next = nullptr;
prev = nullptr;
}
我不确定这是否有帮助,但是我将实现放在下面:
template<class dataType>
Node<dataType>::Node(dataType d)
{
data = d;
next = nullptr;
prev = nullptr;
}
template<class dataType>
bool Node<dataType>::setNext(Node* head)
{
this->next = head;
return true;
}
template<class dataType>
bool Node<dataType>::setPrev(Node* head)
{
this->prev = head;
return true;
}
template<class dataType>
Node<dataType>* Node<dataType>::getNext() const
{
return next;
}
template<class dataType>
Node<dataType>* Node<dataType>::getPrev() const
{
return prev;
}
template<class dataType>
void Node<dataType>::setData(const dataType& d){
this->data = d;
}
template<class dataType>
dataType Node<dataType>::getData() const
{
return data;
}
template<class dataType>
bool Node<dataType>::add(const dataType d){
Node* dummy = new Node(d);
dummy->setNext(this);
this->setPrev(dummy);
dummy->setPrev(nullptr);
this = dummy;
return true;
}
template<class dataType>
bool Node<dataType>::remove(const dataType d){
Node* pointer = this;
while(pointer != nullptr)
{
if(pointer->getData() == d)
{
if(pointer->getPrev() == nullptr)
{
this = pointer->getNext();
pointer = pointer->getNext();
delete pointer->getPrev();
pointer->setPrev(nullptr);
}
else if(pointer->getNext() == nullptr)
{
pointer->getPrev()->setNext(nullptr);
delete pointer;
pointer = nullptr;
}
else
{
pointer->getPrev()->setNext(pointer->getNext());
pointer->getNext()->setPrev(pointer->getPrev());
Node* dummy = pointer;
pointer = pointer->getNext();
delete dummy;
}
}
}
return true;
}
template<class dataType>
Node<dataType>::~Node(){
Node* pointer = this;
Node* holder = this->getNext();
while (holder != nullptr)
{
delete pointer;
pointer = holder;
holder = holder->getNext();
}
delete pointer;
delete holder;
}
这是我使用的测试代码:
int main(){
int v = 10;
string d = "X";
Node<int>* headInt = new Node<int>(v);
Node<string>* headString = new Node<string>();
headString->setData(d);
cout << "headInt contain" << headInt->getData() << ", ";
cout << "headString contain" << headString->getData() << endl << endl;
headInt->add(22);
headString->add("XXII");
cout << "headInt contain" << headInt->getData() << ", ";
cout << "headString contain" << headString->getData() << endl << endl;
headInt->remove(22);
headString->remove("XXII");
cout << "headInt contain" << headInt->getData() << ", ";
cout << "headString contain" << headString->getData() << endl << endl;
}
我希望将其作为结果:
headInt contain 10,
headString contain X
headInt contain 22,
headString contain XXII
headInt contain 10,
headString contain X