如何使此模板类编译
How to get this template class to compile?
对不起,我无法提出一个可以正确捕获我的问题的问题。我的问题是。
我有这样的模板类。我无法理解如何确切定义GET函数。
template<class Data>
class Class
{
struct S
{
};
void Do();
S Get();
};
template<class Data>
void Class<Data>::Do()
{
}
template<class Data>
Class<Data>::S Class<Data>::Get()
{
}
我得到以下错误
1>error C2143: syntax error : missing ';' before 'Class<Data>::Get'
1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>fatal error C1903: unable to recover from previous error(s); stopping compilation
template<class Data>
Class<Data>::S Class<Data>::Get()
需要
template<class Data>
typename Class<Data>::S Class<Data>::Get()
因为 S
是一个因类型。每当您有嵌套在模板中的类型时,都需要使用关键字typename
。例如,vector<int>
上的迭代器具有typename vector<int>::iterator
类型。
a c 11样式,易于读写:
template<class Data>
auto Class<Data>::Get() -> S {
return {};
}
相关文章:
- 如何使此递归函数从给定的起始位置返回最小的整数?
- 使用Visual Studio 2012编译时,此代码会给我错误,但是使用代码块就可以了
- 为什么必须动态分配扩展数组才能使此功能正常工作C++
- 为什么在同一条件变量上使用多个互斥锁会使此代码崩溃?
- 如何使此函数适用于 100 个对象 (c++)?
- 如何使此函数使用 getline 读取字符串并使用 int 执行相同的行为?
- 如何使此 DLL 头文件更可重用
- C++编译错误(在此函数中未初始化)
- 从2011年开始,必须在2019年开始工作以使此Windows Textbox做出哪些更改
- 使此功能更"elegant" C++
- 是否有一种方法可以使此C 14递归模板在C 17中短
- 如何使此功能与 FILE 一起使用?C++
- 有没有更好的方法可以使此代码线程安全?线程局部静态似乎是一个生硬的工具
- 使此 C++ 代码通用
- 如何在C 中使此初始化合法
- 如何使用 openMP 使此代码线程安全
- 如何使此链接列表正常工作
- 如何使此模板类编译
- 如何使此C 程序更强大
- 编译错误:..在此范围内未声明