gpt4 book ai didi

c++ - C++中的泛型编程和类中的typedef

转载 作者:行者123 更新时间:2023-11-27 23:28:42 26 4
gpt4 key购买 nike

假设我在 A.cpp 文件中有以下代码:

template <typename T>
class A{
typedef T myType;
myType foo();
}

如果我想在这个文件中实现foo函数,函数声明的语法是什么?我以为会是:

template <class T>
myType A<T>::foo(){
.
.
.
}

显然这是错误的。

最佳答案

是的,typedef只在类内可用,返回类型不在类内:

template <class T>
typename A<T>::myType A<T>::foo() {}

关于c++ - C++中的泛型编程和类中的typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7512857/

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