我的第一篇文章之一,很抱歉任何格式错误:
我正在尝试创建一个简单的链接列表类,但是每当我尝试运行代码时,我都会遇到错误。我正在编写的另一个代码也没有共享任何独特的特征。我已经搜索了这个网站和其他论坛,大约一个月了,我找不到任何有助于解决方案的东西。这是错误:
架构X86_64的未定义符号:" linkedlist :: linkedlist()",从: _ Main.o中的MainLD:符号(s)架构x86_64找不到符号clang:错误:Linker命令因出口代码1失败(使用-V查看调用)
很抱歉列出了长时间的代码,但我根本无法缩小问题。这是我的主():
#include <iostream>
#include "LinkedListHeader.h"
using namespace std;
// Driver
int main()
{
// LinkedList instance
LinkedList* obj = new LinkedList();
// Prompt user for number of inputs
int count;
cout << "How many items would you like to add to the list?" << endl;
cin >> count;
// For loop
for (int i = 0; i < count; i++)
{
double value;
cout << "Enter item " << i + 1 << ": ";
cin >> value;
obj->add(value);
}
// Call member function displayList() to... wait for it... display the list
obj->displayList();
// Prompt user for a search criteria
double search;
cout << "Enter a number to search for in the list: ";
cin >> search;
// If value is in the list then tell the user. If it ain't then don't
if(obj->isMember(search))
{
cout << search << " is in the list!" << endl;
}
else
cout << search << " is NOT in the list!" << endl;
return 0;
}
linkedlistheader.h:
class LinkedList
{
public:
// LinkedList constructor
LinkedList();
// ListNode struct --> Linked List
struct ListNode
{
double value;
ListNode *next;
// Constructor
ListNode(double value1, ListNode *next1 = nullptr)
{
value = value1;
next = next1;
}
};
// add(double) adds a node to the list with value x
void add(double x)
{
if(head == nullptr)
{
head = new ListNode(x);
}
else
{
ListNode *nodePtr = head;
while(nodePtr->next != nullptr)
{
nodePtr = nodePtr->next;
}
nodePtr->next = new ListNode(x);
}
}
// isMember(x) trverses through list to determine if (value == x)
bool isMember(double x)
{
ListNode *nodePtr = head;
while(nodePtr->next != nullptr)
{
if (nodePtr->value != x)
{
return true;
}
else
return false;
}
return false;
}
// displayList() prints out each member of the list (used to make sure add(double) was working properly)
void displayList()
{
ListNode *nodePtr = head;
while(nodePtr)
{
std::cout << nodePtr->value << " ";
nodePtr = nodePtr->next;
}
std::cout << std::endl;
}
ListNode *head = nullptr;
};
这是我的日志中的完整错误
,因此错误很简单,您没有编写constortor LinkedList::LinkedList()
,但您确实保证要编写它。
class LinkedList
{
public:
// LinkedList constructor
LinkedList();
您的代码中的链接列表构造函数在哪里?编写构造函数,或删除写信的承诺(我认为您可以做后者)。