gpt4 book ai didi

c++ - 在成员函数中使用两个模板的类模板中为成员函数定义单个模板

转载 作者:行者123 更新时间:2023-12-03 06:57:52 25 4
gpt4 key购买 nike

我目前正在学习模板如何在C++中工作。特别是,我正在查看类模板中的单个成员函数模板。要理解我的意思,请在下面找到代码。

// foo.h
template<typename A>
class foo {
template<typename B>
void boo(B);
};

// foo.cpp
template<typename A>
void foo<A>::boo(B value) {} // compiler error: 'Unknown' type name B

// or if I try this

template<typename B>
void foo<A>::boo(B value) {} // compiler error: Use of undeclared identifier A
我正在尝试为该特定功能使用两种类型名称,一种来自类模板,一种来自单个文件模板。但是在上述两个版本中,我遇到了编译器错误。我将如何绕过这个问题?

最佳答案

两组模板参数都需要定义member template
(强调我的)

If the enclosing class declaration is, in turn, a class template, when a member template is defined outside of the class body, it takes two sets of template parameters: one for the enclosing class, and another one for itself:


例如。
template<typename A>
template<typename B>
void foo<A>::boo(B value) {}

关于c++ - 在成员函数中使用两个模板的类模板中为成员函数定义单个模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63896028/

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