When should I use the keyword quot;typenamequot; when using templates(我什么时候应该使用关键字“typename?使用模板时)
问题描述
我最近一直在做一个小项目,但我想不出什么..
I've been working lately on a small project, and I couldn't figure out something..
我得到了一个包含类的 .h 文件,使用了 typename 模板.在那个班级里面有一个私人班级.
I've been given a .h file that was containing a class, using a typename template. Inside that class there was a private class.
template <typename T>
class Something
{
public:
Something();
~Something();
Node* Function1(int index);
int Index(const T& id);
private:
class Node()
{
public:
T id;
//Imagine the rest for the Node
};
};
当我想定义Something"类的函数时出现问题
The problem occured when I wanted to define the functions of the class "Something"
这是我的做法(在 .inl 文件中)
Here's how I was doing it (in a .inl file)
template<typename T>
Node* Something::Function1(int index) //Is the return type well written?
{
// returns the node at the specified index
}
template<typename T>
int Something::Index(const T& id) //Is the parameter type well specified?
{
// returns the index of the node with the specified id
}
所以问题部分是在定义部分......我是否必须告诉编译器返回类型(在这种情况下 Node*)使用 typename 模板(像这样:typename Node*) ?那么参数呢?typename const Node& ?
So the bugging part was in the definitions part... Do I have to tell the compiler that the return type (in this case Node*) uses the typename template (like this: typename Node*) ? And what about the parameter ? typename const Node& ?
所以基本上,我什么时候必须指定函数/参数是否使用模板?
So basically, when do I have to specify wether the function/parameter uses a template?
感谢您的时间.
推荐答案
template<typename T>
typename Something<T>::Node * Something::Function1(int index) //Is the return type well written?
{
// returns the node at the specified index
}
这篇关于我什么时候应该使用关键字“typename"?使用模板时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我什么时候应该使用关键字“typename"?使用模
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
