0

The goal of this code is to sort a grocery list into order by price (and then reverse alpha in the event of equal price) and I can't even test my newly rewritten Add function. I keep getting these errors and cannot for the life of me get past them:

Undefined symbols for architecture x86_64:

"LinkedList::LinkedList()", referenced from:

  _main in main.o

"Node::Node()", referenced from:

  LinkedList::add(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, double) in main.o

"Node::~Node()", referenced from:

  LinkedList::remove(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, double) in main.o

  LinkedList::~LinkedList() in main.o
"Node::~Node()", referenced from:

  LinkedList::~LinkedList() 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)

After doing some research, I've seen a lot of people saying it has to do with missing default constructors but as you can see, I have mine. As for missing symbols, I can't think of anything that isn't there already. I'm still really new to coding (especially in c++) and this deadline is breathing down my neck. Thanks a ton for any help!!


//  main.cpp

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include "LinkedList.hpp"

int main(int argc, const char * argv[]) {

    LinkedList list;

    list.add("Apple", 1.5);
    list.add("Broccoli", 2);
    list.add("Yogurt", 4);
    list.add("Chocolate", 3.8);
    list.add("Paper towels", 2.2);
    list.add("Milk", 1.7);
    list.add("Ice cream", 4.5);
    list.add("Cereal", 4);

    std::cout << "Shopping List: \n";
    list.displayList();
    std::cout << "\n\n";

    return 0;
}

And:

//  LinkedList.hpp

#ifndef LinkedList_hpp
#define LinkedList_hpp

#include <stdio.h>
#include <iostream>
#include <cstdlib>

class Node {
public:

    double icost;
    std::string iname;
    Node *next;
    Node();
    ~Node();
};

class LinkedList : public Node
{
protected:

public:
    int count;
    Node *head;
    Node *tail;
    LinkedList();
    ~LinkedList();              //destructor
    void add(std::string name, double cost);
    void remove(std::string name, double cost);
    void displayList();
};

//------------------------------------------

void LinkedList::add(std::string name, double cost){
    Node *nodePtr;
    Node *previousNodePtr = nullptr;
    bool spotFound;

    Node *incomingNode = new Node();
    incomingNode->iname = name;
    incomingNode->icost = cost;
    incomingNode->next = NULL;

    if (head == NULL){
        head = incomingNode;
        tail = incomingNode;
        count++;
    }

    else{
        nodePtr = head;
        spotFound = false;

        while (!spotFound && nodePtr != NULL){
            if (nodePtr->iname >= incomingNode->iname){
                spotFound = true;
            }
            else {
                previousNodePtr = nodePtr;
                nodePtr = nodePtr->next;
            }
        }
        if (nodePtr == head) {
            incomingNode->next = head;
            head = incomingNode;
            count++;
        }
        else {
            previousNodePtr->next = incomingNode;
            incomingNode->next = nodePtr;
            if (nodePtr == NULL)
                tail = incomingNode;
            count++;
        }
    }
}

//------------------------------------------

void LinkedList::remove(std::string name, double cost){
    Node *nodePtr, *previousNodePtr = NULL;

    if (!head) return;
    if (head->icost == cost){
        nodePtr = head;
        head = head->next;
        delete nodePtr;
    }
    else{
        nodePtr = head;
        while (nodePtr != NULL && nodePtr->icost != cost){
            previousNodePtr = nodePtr;
            nodePtr = nodePtr->next;
        }
        if (nodePtr){
            previousNodePtr->next = nodePtr->next;
            delete nodePtr;
        }
    }
}

//------------------------------------------

void LinkedList::displayList(){
    Node *nodePtr = head;
    while (nodePtr){
        std::cout << nodePtr->iname << ", " << nodePtr->icost << std::endl;
        nodePtr = nodePtr->next;
    }
}

//------------------------------------------

LinkedList::~LinkedList(){
    Node *nodePtr = head;
    while (nodePtr != NULL)
    {
        Node *garbage = nodePtr;
        garbage = nodePtr;
        nodePtr = nodePtr->next;
        delete garbage;
    }
}

//------------------------------------------


#endif /* LinkedList_hpp */

enter image description here

jww
  • 97,681
  • 90
  • 411
  • 885
willysons
  • 1
  • 1
  • 1
    You should also show your compile and link command. Shooting from the hip, add `-stdlib=c++` and `-std=c++11` to your compile command. If your are calling the linker directly, then do the same for your link command. Also see [When is it necessary to use use the flag -stdlib=libstdc++?](https://stackoverflow.com/q/19774778/608639) – jww Feb 22 '18 at 11:30
  • 1
    Where is is your implementation of the Node class ? Where is the implementation of the LinkedList constructor? They have been declared but not defined... Defining in the hpp might cause some issues, have you considered moving the implementation to a dedicated cpp source file? Also @jww has a good point about your compilation/linkage commands... – Rann Lifshitz Feb 22 '18 at 11:31
  • I can't say that I know what you mean for the "compile and link commands"; I clicked on that link you included and still don't understand. As for creating a .cpp - a friend of mine told me that in Xcode you can't have more than one .cpp? Is the wrong or is there a specific way you must add these files in order to get them to talk to each other properly? – willysons Feb 22 '18 at 11:40
  • Welp I resolved all of my errors by just deleting the constructors and destructors entirely so I guess they really aren't that necessary. – willysons Feb 22 '18 at 12:20

0 Answers0