0

I have been writing this class for logging but I haven't been fixing this problem.

mylogger.cpp

#include "../inc/mylogger.h"


MyLogger::MyLogger()
{
    cout << "hi" << endl;
}

template<typename... Args>
void MyLogger::function1(Args... args)
{
    printf( args...);
}

MyLogger MYLOGGER = MyLogger();

mylogger.h

#ifndef MYLOGGER_H
#define MYLOGGER_H

#include <iostream>
#include <iostream>
#include <cstdarg>
#include <string>
#include <thread>

#define LOG_WRITE MYLOGGER.function1

using namespace std;

class MyLogger{
    public:
        MyLogger();
        template<typename... Args> void deneme(Args... args);

};

extern MyLogger MYLOGGER;

#endif

main.cpp

#include <iostream>
#include "../inc/mylogger.h"

using namespace std;


int main(int argc, char** argv)
{

    LOG_WRITE("hello", 3, 4, 1);

    return 0;
}

I got this error. undefined reference to `void MyLogger::function1<char, int, int, int>(char, int, int, int)'

I don't understand. How to fix it?

AHC
  • 21
  • 3
  • 2
    Does this answer your question? [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Nathan Pierson Aug 13 '21 at 14:16

1 Answers1

0

You have implementation of template in other file, the compiler cannot find it and create a specific implementation for certain type. You must create file "templates.cpp" and declare a specific template there. Like on this photo:enter image description here

Or you can set all your implementation of your functions from mylogger.cpp in mylogger.h

THND
  • 217
  • 2
  • 9