Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined Reference to

When I compile my code for a linked list, I get a bunch of undefined reference errors. The code is below. I have been compiling with both of these statements:

g++ test.cpp 

as well as

g++ LinearNode.h LinearNode.cpp LinkedList.h LinkedList.cpp test.cpp  

I really do not understand why I am getting these errors because I am really rusty on classes in C++. I could really use some help.

LinearNode.h:

#ifndef LINEARNODE_H
#define LINEARNODE_H
#include<iostream>

using namespace std;

class LinearNode
{
    public:
        //Constructor for the LinearNode class that takes no arguments 
        LinearNode();
        //Constructor for the LinearNode class that takes the element as an argument
        LinearNode(int el);
        //returns the next node in the set.
        LinearNode* getNext();
        //returns the previous node in the set
        LinearNode* getPrevious();
        //sets the next element in the set
        void setNext(LinearNode* node);
        //sets the previous element in the set
        void setPrevious(LinearNode* node);
        //sets the element of the node
        void setElement(int el);
        //gets the element of the node
        int getElement();

    private: 
        LinearNode* next;
        LinearNode* previous;
        int element;        
};//ends the LinearNode class

#endif

LinearNode.cpp:

#ifndef LINEARNODE_cpp
#define LINEARNODE_cpp
#include<iostream>
#include"LinearNode.h"

using namespace std;

//Constructor for LinearNode, sets next and element to initialized states
LinearNode::LinearNode()
{
    next = NULL;
    element = 0;
}//ends LinearNode default constructor

//Constructor for LinearNode takes an element as argument.
LinearNode::LinearNode(int el)
{
    next = NULL;
    previous = NULL;
    element = 0;
}//ends LinearNode constructor

//returns the next element in the structure
LinearNode* LinearNode::getNext()
{
    return next;
}//ends getNext function

//returns previous element in structure
LinearNode* LinearNode::getPrevious()
{
    return previous;
}//ends getPrevious function

//sets the next variable for the node
void LinearNode::setNext(LinearNode* node)
{
    next = node;
}//ends the setNext function

//sets previous for the node
void LinearNode::setPrevious(LinearNode* node)
{
    previous = node;
}//ends the setPrevious function

//returns element of the node
int LinearNode::getElement()
{
    return element;
}//ends the getelement function

//sets the element of the node
void LinearNode::setElement(int el)
{
    element = el;
}//ends the setElement function

#endif

LinkedList.h:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream>
#include"LinearNode.h"

using namespace std;

class LinkedList
{
    public:
        LinkedList();
        void add(int element);
        int removie (int element);

    private:
        int count;
        LinearNode *contents;
};//ends the class linked list

#endif

LinkedList.cpp:

#ifndef LINKEDLIST_CPP
#define LINKEDLIST_CPP

#include<iostream>
#include"LinearNode.h"
#include"LinkedList.h"

using namespace std;

//linkedlist constructor for an empty linked list
LinkedList::LinkedList()
{
    count = 0;
    contents = NULL;
}//ends the constructor

//adds an element to the front of the linked list
void LinkedList::add(int element)
{
    int found = 0, current = 0;

    while( (found == 0) && (current !=count) )
    {
        if (contents.getElement() == element)
            found = 1;
        else    
        {
            contents = contents.getNext();
            current++;
        }//ends the else statement
    }//ends the while loop

    if (found == 0)
    {
        LinearNode node = new LinearNode(element);
        node.setNext(contents);
        contents.setPrevious(node);
        count++;
    }//ends the found == 0 if statment
}//ends the add function

//this function removes one element from the linked list.
int LinearNode::remove(int element)
{
    int found = 0;

    if (count == 0)
        cout << "The list is empty" << endl;
    else 
    {
        if (contents.getElement() == element)
        {
            result = contents.getElement();
            contents = contents.getNext();
        }//ends the contents.getElement() == element
        else 
        {
            previous = contents;
            current = contents.getNext();
            for (int index = 0; ( (index < count) && (found == 0) )index++)
                if (current.getElement() = element)
                    found = 1;
                else
                {
                    previous = current;
                    current = current.getNext();
                }//ends the else statement 

            if (found == 0)
                cout << "The element is not in the list" << endl;
            else
            {
                result = current.getElement();
                previous.setNext(current.getNext());
            }//ends else statement  

        }//ends the else stamtement

        count--;
    }//ends the else statement of count == 0
    return result;
}//ends the remove function

#endif

test.cpp:

#include<iostream>
#include"LinearNode.h"
#include"LinkedList.h"

using namespace std;

int main()
{

    LinearNode node1, node2, node3, move;
    LinkedList list;    

    node1.setElement(1);
    node2.setElement(2);
    node3.setElement(3);
}   
like image 644
tpar44 Avatar asked Mar 13 '11 22:03

tpar44


People also ask

How do I fix undefined reference error in C++?

You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

How do I fix linker error in C++?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

What does undefined reference to main mean in C++?

Undefined reference to main() means that your program lacks a main() function, which is mandatory for all C++ programs.


4 Answers

  1. Usually headers guards are for header files (i.e., .h ) not for source files ( i.e., .cpp ).
  2. Include the necessary standard headers and namespaces in source files.

LinearNode.h:

#ifndef LINEARNODE_H #define LINEARNODE_H  class LinearNode {     // ..... };  #endif 

LinearNode.cpp:

#include "LinearNode.h" #include <iostream> using namespace std; // And now the definitions 

LinkedList.h:

#ifndef LINKEDLIST_H #define LINKEDLIST_H  class LinearNode; // Forward Declaration class LinkedList {     // ... };  #endif 

LinkedList.cpp

#include "LinearNode.h" #include "LinkedList.h" #include <iostream> using namespace std;  // Definitions 

test.cpp is source file is fine. Note that header files are never compiled. Assuming all the files are in a single folder -

g++ LinearNode.cpp LinkedList.cpp test.cpp -o exe.out 
like image 55
Mahesh Avatar answered Sep 28 '22 04:09

Mahesh


g++ test.cpp LinearNode.cpp LinkedList.cpp -o test 
like image 40
karlphillip Avatar answered Sep 28 '22 03:09

karlphillip


I was getting this error because my cpp files was not added in the CMakeLists.txt file

like image 26
Deepak Sharma Avatar answered Sep 28 '22 04:09

Deepak Sharma


I had this issue when I forgot to add the new .h/.c file I created to the meson recipe so this is just a friendly reminder.

like image 43
Shauli Avatar answered Sep 28 '22 05:09

Shauli