問(wèn)題描述
我最近一直在做一個(gè)小項(xiàng)目,但我想不出什么..
I've been working lately on a small project, and I couldn't figure out something..
我得到了一個(gè)包含類的 .h 文件,使用了 typename 模板.在那個(gè)班級(jí)里面有一個(gè)私人班級(jí).
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
};
};
當(dāng)我想定義Something"類的函數(shù)時(shí)出現(xiàn)問(wèn)題
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
}
所以問(wèn)題部分是在定義部分......我是否必須告訴編譯器返回類型(在這種情況下 Node*)使用 typename 模板(像這樣:typename Node*
) ?那么參數(shù)呢?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&
?
所以基本上,我什么時(shí)候必須指定函數(shù)/參數(shù)是否使用模板?
So basically, when do I have to specify wether the function/parameter uses a template?
感謝您的時(shí)間.
推薦答案
template<typename T>
typename Something<T>::Node * Something::Function1(int index) //Is the return type well written?
{
// returns the node at the specified index
}
這篇關(guān)于我什么時(shí)候應(yīng)該使用關(guān)鍵字“typename"?使用模板時(shí)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!