Is it possible to transfer type to C++ method or determine type in the method?

  c++

I am trying to implement a list of data of any type by myself, and I want to print this list based on the data type. At this point, I have tested integers and characters. To print a list of these types, there are two methods print_int and print_char, depending on the data type being displayed. My question is, is it possible to define only one method print that will print depending on the data type of my list without creating a class template (unlike List<type> ...)?
I mean without this class definition:

template <class T>
class List{
  T *data;
  List *next;
  ...

(I don’t use the class template because in the case of type void * for data it is possible to mix integers, characters, strings, etc. )

Or maybe I can pass a data type as an argument or determine type in the method?

I have tried using the template from C++ pass variable type to function only for the function print in this form:

template <typename T>
void List::print()
{
    printf("nlist %p: [", this);
    bool is_first = true;
    
    List* element = this;
    while(element)
    {
        if (is_first)
            is_first = !is_first;
        else
            printf(", ");
        printf("%i", *(T*)element->data);
        element = element->next;
    }
    printf("]n");
}

but I got an error when using g++ compiler:

undefined reference to `void List::print<int>()’

collect2: error: ld returned 1 exit status

Here are my files:
main.cpp

#include "list/list.h"
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
    List myList(new int(10));
    myList.append(new int(120));
    myList.append(new int(56));
    
myList.print<int>(); //!
    myList.print_int();
    

    List myListChar(new char('f'));
    myListChar.append(new char(48));
    myListChar.append(new char('g'));

    myListChar.print_int();
    myListChar.print_char();

    return 0;
}

realization of list (list.h):

#ifndef LIST_H
#define LIST_H

class List
{
    void *data;
    List *next;
public:
    List(void*);

    void append(void*);
    void print_int();
    void print_char();
    template <typename T> void print();
};

#endif // LIST_H

list.cpp:

#include "list.h"
#include <stdio.h>

List::List()
{
    data = NULL;
    next = NULL;
}

List::List(void *data)
{
    this->data = data;
    next = NULL;
}

void List::append(void *data)
{
    List *last_element, *element = this;
    while(element)
    {
        last_element = element;
        element = element->next;
    }

    List* next_element = new List(data);
    last_element->next = next_element;
}

void List::print_int()
{
    printf("nlist %p: [", this);
    bool is_first = true;
    
    List* element = this;
    while(element)
    {
        if (is_first)
            is_first = !is_first;
        else
            printf(", ");
        printf("%i", *(int*)element->data);
        element = element->next;
    }
    printf("]n");
}

void List::print_char()
{
    printf("nlist %p: [", this);
    bool is_first = true;
    
    List* element = this;
    while(element)
    {
        if (is_first)
            is_first = !is_first;
        else
            printf(", ");
        printf("%c", *(char*)element->data);
        element = element->next;
    }
    printf("]n");

template <typename T>
void List::print()
{
    printf("nlist %p: [", this);
    bool is_first = true;
    
    List* element = this;
    while(element)
    {
        if (is_first)
            is_first = !is_first;
        else
            printf(", ");
        printf("%i", *(T*)element->data);
        element = element->next;
    }
    printf("]n");

}

Sorry for the stupid question and thank you in advance!

Source: Windows Questions C++

LEAVE A COMMENT