这是我的模板功能
template<typename T> std::stringstream logging_expansion ( T const * value ){
std::stringstream retval;
retval = retval << *value;
return retval;
}
以下是我称之为使用它的方式
logging_expansion( "This is the log comes from the convinient function");
但是链接器告诉我它不能引用该函数:
Undefined symbols for architecture x86_64:
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> > logging_expansion<char>(char const*)", referenced from:
_main in WirelessAutomationDeviceXpcService.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
答案 0 :(得分:5)
您需要在头文件中提供模板函数的实现,或者在头文件中定义特化。
我假设你现在有类似的东西:
//header.h
template<typename T> std::stringstream logging_expansion ( T const * value );
//implementation.cpp
#include "header.h"
template<typename T> std::stringstream logging_expansion ( T const * value ){
std::stringstream retval;
retval = retval << *value;
return retval;
}
//main.cpp
#include "header.h"
//....
logging_expansion( "This is the log comes from the convinient function");
//....
所以你需要将实现移到标题:
//header.h
template<typename T> std::stringstream logging_expansion ( T const * value ){
std::stringstream retval;
retval = retval << *value;
return retval;
}
答案 1 :(得分:0)
您的代码有几个问题:
*我认为您不打算只将字符串的第一个字符添加到您的流中
所以*value
没有多大意义
retval = retval << *value
也没有意义。缺少模板函数的常见原因是您忘记在.cpp文件中实例化模板。你这样做
template std::stringstream logging_expansion<char>( char const *value);