【问题标题】:C++ class function template and variable arguments problem [duplicate]C ++类函数模板和变量参数问题[重复]
【发布时间】:2021-10-16 18:57:40
【问题描述】:

我一直在写这个类来记录日志,但我一直没有解决这个问题。

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

ma​​in.cpp

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

using namespace std;


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

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

    return 0;
}

我收到了这个错误。 对 `void MyLogger::function1(char, int, int, int)'的未定义引用

我不明白。如何解决?

【问题讨论】:

标签: c++ function class templates arguments


【解决方案1】:

您在其他文件中有模板的实现,编译器找不到它并为特定类型创建特定的实现。您必须创建文件“templates.cpp”并在那里声明一个特定的模板。喜欢这张照片:

或者您可以在 mylogger.h 中的 mylogger.cpp 中设置所有函数的实现

【讨论】:

    猜你喜欢
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多