C++ 模板可变参数函数未定义引用

hup*_*yuy 2 c++ templates variadic

测试温度B.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_ */
Run Code Online (Sandbox Code Playgroud)

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 */
Run Code Online (Sandbox Code Playgroud)

主要的

#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*)'
}
Run Code Online (Sandbox Code Playgroud)

我正在使用可变参数模板。为什么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
}
Run Code Online (Sandbox Code Playgroud)

为什么以及如何解决它?谢谢!!!

ikh*_*ikh 5

不能将模板函数放入.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*)'
}
Run Code Online (Sandbox Code Playgroud)

等于

#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*)'
}
Run Code Online (Sandbox Code Playgroud)

现在,假设您是编译器。你正在尝试编译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*)'
}
Run Code Online (Sandbox Code Playgroud)

现在您确实知道是什么callCommandA了,然后您可以专门化并编译它。