0

I am trying to create a c++ Logger, but I get 2 errors when I try to compile it. I tried to find a solution, but I didn't manage to find one. The only think I managed to find out is that it is a linking error, but I have no idea how to fix it.

Here is my code:

main.cpp

#include "logger.h"

int main()
{
    Logger::Debug("h", "dog");
    return 0;
}

logger.h

#pragma once

#include <iostream>

class Logger
{
private:

    template<typename... Args>
    static void Log(Args... args);

public:

    template<typename... Args>
    static void Debug(Args... args);

    template<typename... Args>
    static void Info(Args...args);
};

logger.cpp

#include "logger.h"

template<typename... Args>
static void Logger::Log(Args... args)
{
    for (auto&& x : { args... })
    {
        std::cout << x << " ";
    }
    std::cout << std::endl;
}

template<typename... Args>
static void Logger::Debug(Args... args)
{
    std::cout << "[Debug]\t";
    Logger::Log(args...);
}

template<typename... Args>
static void Logger::Info(Args... args)
{
    std::cout << "[Info]\t";
    Logger::Log(args);
}

And here are my errors:

errors

Please help. Thanks.

  • 2
    A must read: [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) – user4581301 Jul 06 '21 at 17:28
  • Recommendation: Don't use images to provide error messages. [There are many reasons for this](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – user4581301 Jul 06 '21 at 17:29

0 Answers0