【发布时间】:2011-05-12 18:52:36
【问题描述】:
我最近一直在做一个小项目,但我想不通..
我得到了一个 .h 文件,其中包含一个使用类型名模板的类。在那个班级里面有一个私人班级。
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”类的功能时出现问题
这是我的做法(在 .inl 文件中)
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 Node*)?那么参数呢? typename const Node&?
所以基本上,我什么时候必须指定函数/参数是否使用模板?
感谢您的宝贵时间。
【问题讨论】:
-
请在您的问题中发布sinppets!
-
我已经发布了带有 sn-ps 的链接。我不知道如何正确缩进我的 sn-ps。
标签: c++ templates syntax typename