0

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

This is the full error in my log

user4581301
  • 29,019
  • 5
  • 26
  • 45
Z0S0
  • 5
  • 1
  • 1
    Suggestion: Write less code before testing. if you had tested back when the only code you had was `int main() { LinkedList* obj = new LinkedList(); }` and `class LinkedList { public: LinkedList(); };` you would have had close to zero difficulty narrowing down the problem.` – user4581301 Feb 28 '19 at 22:47
  • Understood, I'll keep that in mind moving forward. Thank you. – Z0S0 Feb 28 '19 at 22:49
  • 1
    Suggestion: save `new` for when you really need it. `LinkedList obj;` would give you the same linked list without you having to manually manage a dynamic allocation. More on the subject: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Feb 28 '19 at 22:49

1 Answers1

0

So the error is simple, you didn't write the constructor LinkedList::LinkedList() but you did promise to write it.

class LinkedList
{
public:
// LinkedList constructor
LinkedList();

Where in your code is the linked list constructor? Either write the constructor, or delete the promise to write it (I think you can probably do the latter).

john
  • 71,156
  • 4
  • 49
  • 68