c++ - 由于继承,模板化函数的多个函数定义
问题描述
由于使用模板化参数和继承的函数,我无法使用 VS2015 编译我的程序。
错误就是这个。
我正在努力实现以下目标:
class A
{
//do something
};
class B : public A
{
//do something
};
template <typename T>
class Foo {
template <typename T>
friend void function(Foo<T> & sm) {
//do something
}
};
void main()
{
Foo<A> test;
Foo<B> test2;
};
我确实理解错误的含义,但我不明白它为什么会发生。
我想function
是用两个不同的签名创建的:
void function(Foo<A> & sm);
和
void function(Foo<B> & sm);
多定义怎么算?
编辑 - 完整的错误信息:
Error C2995 'void function(Foo<T> &)': function template has already been defined
解决方案
Clang 和 MS 都有同样的抱怨。删除第二个模板说明符,它将编译。
class A{};
class B : public A{};
template <typename T>
class Foo {
// template <typename T>
friend void function(Foo<T> & sm) {
}
};
int main()
{
Foo<A> test;
Foo<B> test2;
};
T
已经为类指定了Foo
它的友元函数。template
如果功能有差异,您会稍等一下,例如:
class A{};
class B : public A{};
template <typename T>
class Foo {
template <typename U>
friend void function(Foo<T> & sm, U another) {
}
};
int main()
{
Foo<A> test;
Foo<B> test2;
};
推荐阅读
- html - 在 Vue 中使用自定义 HTML 标签而不使用 v-pre 或 ignoreElements
- shell - 如何剪切由变量定义的一系列行
- javascript - 我的导航切换器图标未显示,但在单击该区域时它工作正常。我能做些什么来解决这个问题
- c# - 调用 async EventHandler 的区别
- ruby-on-rails - 使用 find .where 后如何获取活动记录的名称
- java - Java Servlet 如何通过 Session 获取 UserId
- python - 如何使用pyaudio在macos上录制麦克风?
- flutter - 无法在 macOS Catalina 10.15.4 上安装 dart
- javascript - Html Webpack Plugin 呈现 JS 模块而不是 HTML 文件
- c++ - 如何将一个类的成员函数作为参数传递给另一个类的成员函数?