Undefined Reference C++ (Queue Data Structure) [duplicate]

  c++, data-structures, file-handling, queue

I am currently writing a program that will store the customer information into a Queue and into a text file. However, I couldn’t run the program. It was all working when I defined my functions in my Data.h file, but when I transferred the body of my functions to another CPP file, it started to create an undefined reference error to ‘Queue<Customer_Information>::push(Customer_Information)’ from the my Main.cpp file.

Here is my Main.cpp file:

#include <iostream>
#include "Data.h"
using namespace std;

//All of this is related to Customers' Details Menu
void Menu(Queue<Customer_Information>& link) {
    Customer_Information Cinfo;
    //Push Function Test
    cout << "nEnter the Name: ";
    getline(cin, Cinfo.Name);
    cout << "Enter the Address: ";
    getline(cin, Cinfo.Address);
    link.push(Cinfo);
    //Related to File Handling
    link.Begin_Record();
    //Saving details to text file
    link.Record_Data(Cinfo);
    
    cout << "Saving details to "Customers Information.txt"" << endl;
    
    //Displaying the data from the text file to console
    cout << "nReading from text file" << endl << endl;
    link.TextToConsole();
    
    //Pop Function Test
    cout << "nPop Function" << endl;
    link.pop();
    
}

int main () {
    
    Queue<Customer_Information> link;
    Menu(link);

    return 0;
}

This is my Data.h file:

#pragma once
#include <fstream>
using namespace std;

struct Customer_Information {
    string Name;
    string Address;
    string ID;
};

template <class T>
class Queue {
    private:
            T value;
            Queue *next;
            
    Queue *front, *rear;
    public:

Queue();
void push(T data);
void pop();
void Record_Data(T data);
void Begin_Record();
void TextToConsole();
};

I created a separate File_Handling_Implementation file for the body of my file handling functions:

#include <iostream>
#include "Data.h"
#include <fstream>
using namespace std;

struct Temp_File {
    string data;
    string type;
};

template <class T>
void Queue<T>::Begin_Record() {
    fstream FILE;
    FILE.open("Customers Information.txt", ios::out | ios::in | ios::app);
    if (!FILE) {
        FILE << "Nothing to open!" << endl;
    }
    else {
        FILE << "Customers Information" << endl;
    }
}

template <class T>
void Queue<T>::Record_Data(T data) {
    fstream FILE;       
    FILE.open("Customers Information.txt", ios::out | ios::in | ios::app);
            
    if (!FILE) {
        cout << "Nothing to open!" << endl;
    }
    else {
        FILE << "Name: " << data.Name << endl;
        FILE << "Address: " << data.Address << endl << endl;
    }
}

template <class T>
void Queue<T>::TextToConsole() {
    Temp_File t;
            fstream FILE;
            char delimiter = ':';
            char delimiter1 = 'n';
            FILE.open("Customers Information.txt", ios::in);
            
            if(FILE.is_open()){
                while(getline(FILE,t.type,delimiter) && getline(FILE,t.data,delimiter1)){

                cout << t.type <<": ";
                cout << t.data <<" ";
                cout << endl;

                }
                FILE.close();
            }
            else {
                cout << "file is not open."; 
            }
            cin.get();
    }

Another separate CPP file for the queue functions:

#include <iostream>
#include "Data.h"
using namespace std;

template <class T>
Queue<T>::Queue() {
    front = NULL;
    rear = NULL;
}

template <class T>
void Queue<T>::push(T data) {
    Queue *tmp = new Queue;
    tmp->value = data;
    tmp->next = NULL;
    if (!rear) {
        rear = tmp;
        front = tmp;
    }
    else {
        rear->next = tmp;
        rear = tmp;
    }
}

template <class T>
void Queue<T>::pop() {
    if (front != NULL) {
        Queue *tmp;
        tmp = front;
        cout << front->value.Name << " deleted" << endl;
        front = front->next;
        delete tmp;
        if (!front) {
            rear = NULL;
        }
    }
    else {
        cout << "Nothing to delete!";
    }
}

The error messages are:

Main.cpp:(.text+0x94): undefined reference to
Queue<Customer_Information>::push(Customer_Information)' Main.cpp:(.text+0xac): undefined reference to Queue<Customer_Information>::Begin_Record()’ Main.cpp:(.text+0xd6):
undefined reference to
Queue<Customer_Information>::Record_Data(Customer_Information)' Main.cpp:(.text+0x18c): undefined reference to Queue<Customer_Information>::push(Customer_Information)’
Main.cpp:(.text+0x1c0): undefined reference to
Queue<Customer_Information>::Record_Data(Customer_Information)' Main.cpp:(.text+0x230): undefined reference to Queue<Customer_Information>::TextToConsole()’ Main.cpp:(.text+0x25d):
undefined reference to Queue<Customer_Information>::pop()' Main.cpp:(.text+0x2e5): undefined reference to Queue<Customer_Information>::Queue()’

Source: Windows Questions C++

LEAVE A COMMENT