c++模板变量函数未定义引用

c++ template variadic function undefined reference

本文关键字:未定义 引用 函数 变量 c++      更新时间:2023-10-16

TestTempB.h

#ifndef TESTTEMPB_H_
#define TESTTEMPB_H_
#include <string>
using namespace std;
namespace ddl_lib {
class TestTempB {
public:
    TestTempB();
    virtual ~TestTempB();
    template<class ... Args>
    void callCommandB(const string&, const uint32_t*, Args ...);
    template<class ... Args>
    void callCommandA(const string&, Args ...);
};
} /* namespace ddl_lib */
#endif /* TESTTEMPB_H_ */

TestTempB.cpp

#include "TestTempB.h"
#include "TestTempA.h"
#include "stdint.h"
#include <iostream>
namespace ddl_lib {
TestTempB::TestTempB() {
    // TODO Auto-generated constructor stub
}
TestTempB::~TestTempB() {
    // TODO Auto-generated destructor stub
}
template<class ... Args>
void TestTempB::callCommandB(const string& cmdName, const uint32_t* cmdSn, Args ... params) {
    cout << cmdName << endl;
}
template<class ... Args>
void TestTempB::callCommandA(const string& cmdName, Args ... params) {
    callCommandB(cmdName, NULL, params...);
}
} /* namespace ddl_lib */

#include <iostream>
#include "TestTempB.h"
using namespace std;
using namespace ddl_lib;
int main() {
    string aa="15615";
    bool ccc=false;
    TestTempB b;
    b.callCommandA("name", &aa, &ccc);//undefined reference to `void ddl_lib::TestTempB::callCommandA<std::string*, bool*>(std::string const&, std::string*, bool*)'
}

我使用的是变分法模板。为什么gcc不能推导出真正的函数?当在主文件中调用callCommandA和callCommandB时,它工作得很好。

#include <iostream>
#include "TestTempB.h"
using namespace std;
using namespace ddl_lib;
template<class ... Args>
void callCommandB(const string& cmdName, const uint32_t* cmdSn, Args ... params) {
    cout << cmdName << endl;
}
template<class ... Args>
void callCommandA(const string& cmdName, Args ... params) {
    callCommandB(cmdName, NULL, params...);
}
int main() {
    string aa = "15615";
    bool ccc = false;
    callCommandA("name", &aa, &ccc); //no erro
}

为什么以及如何修复?谢谢

CANNOT将模板函数放入.cpp。您必须将它放入.h中,因为编译器在专门化它时必须知道模板是什么

您的第一个主文件

#include <iostream>
#include "TestTempB.h"
using namespace std;
using namespace ddl_lib;
int main() {
    string aa="15615";
    bool ccc=false;
    TestTempB b;
    b.callCommandA("name", &aa, &ccc);//undefined reference to `void ddl_lib::TestTempB::callCommandA<std::string*, bool*>(std::string const&, std::string*, bool*)'
}

等于

#include <iostream>
#include <string>
using namespace std;
namespace ddl_lib {
class TestTempB {
public:
    TestTempB();
    virtual ~TestTempB();
    template<class ... Args>
    void callCommandB(const string&, const uint32_t*, Args ...);
    template<class ... Args>
    void callCommandA(const string&, Args ...);
};
} /* namespace ddl_lib */
using namespace ddl_lib;
int main() {
    string aa="15615";
    bool ccc=false;
    TestTempB b;
    b.callCommandA("name", &aa, &ccc);//undefined reference to `void ddl_lib::TestTempB::callCommandA<std::string*, bool*>(std::string const&, std::string*, bool*)'
}

现在想想,就好像你是编译器一样。您正在尝试编译b.callCommandA("name", &aa, &ccc)。你必须专门化模板,但你不知道它到底是什么。要编译它,你必须知道,就像这样:

#include <iostream>
#include <string>
using namespace std;
namespace ddl_lib {
class TestTempB {
public:
    TestTempB();
    virtual ~TestTempB();
    template<class ... Args>
    void callCommandB(const string&, const uint32_t*, Args ...);
    template<class ... Args>
    void callCommandA(const string&, Args ...);
};
template<class ... Args>
void TestTempB::callCommandB(const string& cmdName, const uint32_t* cmdSn, Args ... params) {
    cout << cmdName << endl;
}
template<class ... Args>
void TestTempB::callCommandA(const string& cmdName, Args ... params) {
    callCommandB(cmdName, NULL, params...);
}
} /* namespace ddl_lib */
using namespace ddl_lib;
int main() {
    string aa="15615";
    bool ccc=false;
    TestTempB b;
    b.callCommandA("name", &aa, &ccc);//undefined reference to `void ddl_lib::TestTempB::callCommandA<std::string*, bool*>(std::string const&, std::string*, bool*)'
}

现在您已经知道callCommandA是什么了,然后可以对其进行专门化和编译。