gpt4 book ai didi

c++ - 模板化方法的未解决符号特化

转载 作者:行者123 更新时间:2023-11-27 23:47:36 25 4
gpt4 key购买 nike

此代码无法解析 void K::func<B>(B&) .

struct K {
template <typename T> void func(T &t);
};

struct B {
void ok();
};

// definition
template <typename T> void K::func(T &t) { t->ok(); }

// getting: undefined reference to `void K::func<B>(B&)'
// nm says ' U void K::func<B>(B&)'
template <> void K::func<B>(B &b);
template <> void K::func(B &b);

int main() {
K k;
B b;
k.func(b);
};

注意:SO 的其他几个问题与编译有关;我我卡在链接上了。如果有的话,请指点我复制。

更新:我正在关注 http://en.cppreference.com/w/cpp/language/template_specialization , 寻找函数 g1 或 g2 ;

更新 2:将“func”固定为“K::func”以修复“类成员模板定义之外”,但 K::func 仍然无法专门化。

最佳答案

您需要提供这样的定义:

template <> void K::func<B>(B &b) {}

另外下面两个只有在第二种情况下是等价的,T是从参数类型推导出来的:

template <> void K::func<B>(B &b);
template <> void K::func(B &b);

关于c++ - 模板化方法的未解决符号特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49345492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com