Note that there are some explanatory texts on larger screens.

plurals
  1. POC++, Xcode. Same error every time i try and use classes
    text
    copied!<p>Every time I have a class in a header file and I'm using the class in the source file, I get the same error. Doesn't matter which class or which project it is.</p> <p>I am trying to insert a new node onto the head of a linked list data structure.</p> <p>Right now I have a pretty simple header file <code>main.h</code>:</p> <pre><code>namespace linkedlistofclasses { class Node { public: Node(); Node(int value, Node *next); //Constructor to initialize a node int getData() const; //Retrieve value for this node Node *getLink() const; //Retrieve next Node in the list void setData(int value); //Use to modify the value stored in the list void setLink(Node *next); //Use to change the reference to the next node private: int data; Node *link; }; typedef Node* NodePtr; } </code></pre> <p>My source file <code>main.cpp</code> looks like this:</p> <pre><code>#include &lt;iostream&gt; #include "main.h" using namespace std; using namespace linkedlistofclasses; void head_insert(NodePtr &amp;head, int the_number) { NodePtr temp_ptr; //The constructor sets temp_ptr-&gt;link to head and //sets the data value to the_number temp_ptr = new Node(the_number, head); head = temp_ptr; } int main() { NodePtr head, temp; //Create a list of nodes 4-&gt;3-&gt;2-&gt;1-&gt;0 head = new Node(0, NULL); for (int i = 1; i &lt; 5; i++) { head_insert(head, i); } //Iterate through the list and display each value temp = head; while (temp !=NULL) { cout &lt;&lt; temp-&gt;getData() &lt;&lt; endl; temp = temp-&gt;getLink(); } //Delete all nodes in the list before exiting //the program. temp = head; while (temp !=NULL) { NodePtr nodeToDelete = temp; temp = temp-&gt;getLink(); delete nodeToDelete; } return 0; } </code></pre> <p>My problem is that I get these compilation errors: </p> <pre><code>Undefined symbols for architecture x86_64: "linkedlistofclasses::Node::Node(int, linkedlistofclasses::Node*)", referenced from: head_insert(linkedlistofclasses::Node*&amp;, int) in main.o _main in main.o "linkedlistofclasses::Node::getData() const", referenced from: _main in main.o "linkedlistofclasses::Node::getLink() const", 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) </code></pre> <p>If I run the code without a using a class, writing everything in the source file <code>main.cpp</code>, there is no problem. But no matter how I write a class, I always get some variant of this error. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload