c++模板变量函数未定义引用
c++ template variadic function undefined reference
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
是什么了,然后可以对其进行专门化和编译。
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用