Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何显式实例化可变模板函数?C++;_C++ - Fatal编程技术网

C++ 如何显式实例化可变模板函数?C++;

C++ 如何显式实例化可变模板函数?C++;,c++,C++,h文件: 示例函数: template<typename... Args> std::string print(const char *format, Args... args); cpp文件: template<typename... Args> string print(const char *format, Args... args) { return string; } template<typename... Args> string p

h文件:

示例函数:

template<typename... Args>
std::string print(const char *format, Args... args);
cpp文件:

template<typename... Args>
string print(const char *format, Args... args) {
    return string;
}
template<typename... Args>
string print(const char *format, Args... args) {
    return string;
}

template std::string print(const char *, const char *);
template std::string print(const char *, const char *, int);
模板
字符串打印(常量字符*格式,Args…Args){
返回字符串;
}
我明白了

架构x86_64的未定义符号


实例化声明很好,但定义也需要实例化。将以下内容添加到您的cpp文件:

template<typename... Args>
string print(const char *format, Args... args) {
    return string;
}

template std::string print(const char *, const char *);
template std::string print(const char *, const char *, int);
模板
字符串打印(常量字符*格式,Args…Args){
返回字符串;
}
模板std::字符串打印(常量字符*,常量字符*);
模板std::字符串打印(常量字符*,常量字符*,int);

现在,这些函数也将被定义。如果您正确进行链接,则链接应该可以找到它们。

除了复制粘贴(而不是转述)错误消息外,请提供其他错误消息。为什么
外部
?这看起来像是一个打字错误,说它是在其他地方声明的。@MatthieuBrucher-你实例化了一个声明。定义在哪里实例化?@StoryTeller的确,这个词错了,我想说定义了。谢谢!实际上,声明似乎根本不需要实例化?@iamcencrypted——严格来说,不需要。但它有一个文档化的目的。我认为这是一个糟糕的答案