LD 找不到生成的函数
ld cannot find generated function
我的代码:
#include <string>
template <typename ...KVs>
const std::string BuildJson(KVs... kvs);
template <typename ...AnotherKVs>
const std::string BuildJson(const std::string& key, const char* value, const std::string& nextkey, AnotherKVs... kvs)
{
std::string building = """ + key + "": ";
building += (value + ',' + BuildJson(nextkey, kvs...));
return building;
}
template <typename T, typename ...AnotherKVs>
const std::string BuildJson(const std::string& key, T value, const std::string& nextkey, AnotherKVs... kvs)
{
std::string building = """ + key + "": ";
building += (std::to_string(value) + ',' + BuildJson(key, kvs...));
return building;
}
template <>
const std::string BuildJson(const std::string& key, const char* value)
{
std::string building = """ + key + "": ";
building += value;
return building;
}
template <typename T>
const std::string BuildJson(const std::string& key, T value)
{
std::string building = """ + key + "": ";
building += std::to_string(value);
return building;
}
template <>
const std::string BuildJson() { return std::string(); }
int main(int argc, char* argv[])
{
BuildJson(
"key1", "value1",
"key2", 10000,
"key3", 10000.6999
);
return 0;
}
/usr/bin/ld: /tmp/ccReTAIo.o: in function `main':
template_build_json.cpp:(.text+0x11c): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const BuildJson<char const*, char const*, char const*, int, char const*, double>(char const*, char const*, char const*, int, char const*, double)'
collect2: error: ld returned 1 exit status
如果把BuildJson(const std::string& key, T value)
放在BuildJson(const std::string& key, const char* value)
G++ 之前,G++ 会说:
template_build_json.cpp:33:19: error: ambiguous template specialization ‘BuildJson<>’ for ‘const string BuildJson(const string&, const char*)’
33 | const std::string BuildJson(const std::string& key, const char* value)
为什么ld找不到刚刚生成的函数?
包可以是空的。
取而代之的是:
template <typename ...AnotherKVs>
const std::string BuildJson(const std::string& key, const char* value, AnotherKVs... kvs)
template <typename T, typename ...AnotherKVs>
const std::string BuildJson(const std::string& key, T value, AnotherKVs... kvs)
尝试这样的事情:
template <typename... Tail>
const std::string BuildJson(const std::string& key, const char* value, const std::string& nextKey, Tail... tail)
template <typename T, typename... Tail>
const std::string BuildJson(const std::string& key, T value, const std::string& nextKey, Tail... tail)
例如
building += (std::to_string(value) + ',' + BuildJson(nextKey, tail...));
这要求这些"有更多参数"的情况确实具有更多参数(至少一个,nextKey
参数(。
还要考虑完美转发,返回const
值或多或少毫无意义。
相关文章:
- 编译器找不到'aligned_alloc'函数
- 在 c++ 中找不到此函数的标识符
- LLVM 选择找不到函数传递
- 找不到 #define 的函数定义
- LD 找不到生成的函数
- C++线程找不到函数作为参数(链接器)
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- Javascript 找不到使用 emscripten 编译的导出 WASM 函数
- 给定一个 3 x 3 矩阵或任何 n x n 矩阵,为什么我的以下函数找不到对角线差异?
- QObject::连接不起作用 - 使用函数语法找不到信号
- VS 2017 C++编译器在 VS 2005 中找不到匹配函数
- 找不到如何创建使用指针、字符和 for 函数组合的程序
- 这个基于范围的'for'语句需要一个合适的函数,但找不到任何函数
- im调用函数时找不到标识符?
- 找不到使用 bitset 实例化模板函数的有效方法
- 如何修复找不到匹配的重载函数?
- ADL 找不到重载函数
- 构造函数找不到合适的定义,以及 100 个其他错误?
- 类函数找不到类定义的变量
- 平均函数找不到正确的平均c++