Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:52:14+00:00 2026-06-12T17:52:14+00:00

Every time I have a class in a header file and I’m using the

  • 0

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.

I am trying to insert a new node onto the head of a linked list data structure.

Right now I have a pretty simple header file main.h:

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

My source file main.cpp looks like this:

#include <iostream>
#include "main.h"

using namespace std;
using namespace linkedlistofclasses;

void head_insert(NodePtr &head, int the_number) {

    NodePtr temp_ptr;
    //The constructor sets temp_ptr->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->3->2->1->0
    head = new Node(0, NULL);

    for (int i = 1; i < 5; i++) {
        head_insert(head, i);
    }

    //Iterate through the list and display each value 
    temp = head;
    while (temp !=NULL) {
        cout << temp->getData() << endl;
        temp = temp->getLink();
    }

    //Delete all nodes in the list before exiting
    //the program.
    temp = head;
    while (temp !=NULL) {
        NodePtr nodeToDelete = temp;
        temp = temp->getLink();
        delete nodeToDelete;
    }

    return 0;
}

My problem is that I get these compilation errors:

Undefined symbols for architecture x86_64:
  "linkedlistofclasses::Node::Node(int, linkedlistofclasses::Node*)", referenced from:
      head_insert(linkedlistofclasses::Node*&, 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)

If I run the code without a using a class, writing everything in the source file main.cpp, there is no problem.
But no matter how I write a class, I always get some variant of this error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:52:15+00:00Added an answer on June 12, 2026 at 5:52 pm

    You are merely declaring the following class methods:

    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
    

    Which allows those who include main.h to see the existence of linkedlistofclasses::Node, as well as its public members. This way, you are able to call them, for example, from the main() function.

    However, as they are simply declared and not defined, this raises issues later on:

    **Undefined symbols for architecture x86_64:
    "linkedlistofclasses::Node::Node(int, linkedlistofclasses::Node*)", referenced from:
      head_insert(linkedlistofclasses::Node*&, 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**
    

    Those error messages you got are occurring because the linker does not know the region in the internal representation of the program where the code associated to those method names resides. And it can’t be found because you did not define it in the first place! Consider the situation: what sense does it make to call a function whose code is non-existent?

    May I suggest that you create a Node.h and Node.cpp files, the first with declarations and the second with definitions (again, differences between the two concepts), and then #include Node.h from main.c?

    In the current situation, you will notice that if additionally you invoke any of these:

    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
    

    Their names will be added to the message errors you’re already receiving!
    Also, to improve legibility, may I recommend changing the namespace to LinkedListOfClasses?

    EDIT: regarding your comment about what you posted in pastebin:

    // Node.h
    namespace LinkedListOfClasses {
      class Node {
    
      public:
          Node();
          Node(int value, Node *next);
          int getData() const;
          Node *getLink() const;
          void setData(int value);
          void setLink(Node *next);
      private:
          int data;
          Node *link;
      };
      typedef Node* NodePtr;
    }
    

    The code above contains the declarations of the methods you mentioned in your comment. Regarding the missing definition of the following methods declared in the Node class:

    int getData() const;
    Node *getLink() const;
    

    You want to include their definitions in your Node**.cpp** file, not the .h! The Node.cpp would then look like this:

    // Node.cpp
    #include "Node.h"
    
    using namespace LinkedListOfClasses;
    
    void head_insert(NodePtr &head, int the_number) {
    
        NodePtr temp_ptr;
        temp_ptr = new Node(the_number, head);
        head = temp_ptr; 
    }
    
    Node::Node(int value, Node *next) {  
    }
    
    void Node::setData(int value) {
    }
    
    void Node::setLink(Node *next) {
    }
    
    int Node::getData() const {
      // getData definition was missing, now it's defined in Node.cpp!
    }
    Node *Node::getLink() const {
      // getLink definition was missing, now it's defined in Node.cpp!
    }
    

    Hope I managed to be of some help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
I have a Products:List<Product> class. I'd like to make it so that every time
In Visual studios, is the class recalled every time the page refreshes? I have
I find this syntax astoundingly annoying. Every time I rename my class, I have
I am beginning to hate objects in javascript. Every time I have error and
I need to have a newline every time I write to a file in
I have this definition in a header file: class Owner { private: // Fields
As an amateur mobile developer, I feel dismay every time I have to fix,
I have this warning every time I run my CGI-script (output is rendered by
I have a color block and every time I press on it I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.