One of my first posts so sorry for any formatting errors:
I'm trying to create a simple Linked List Class but I keep getting an error whenever I try to run my code. I am getting the same error with another code I'm writing that doesn't share any unique characteristics as well. I've been searching this site and other forums for about a month now and I can't find anything that helps with a solution. This is the Error:
Undefined symbols for architecture x86_64: "LinkedList::LinkedList()", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Sorry for the long code listing but I couldn't narrow down the problem at all. Here is my main():
#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;
};