模板类 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4573952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
template class c++
提问by inna karpasas
i try to design a template for my university project. i wrote the follwing code:
我尝试为我的大学项目设计一个模板。我写了以下代码:
#ifndef _LinkedList_H_
#define _LinkedList_H_
#include "Link.h"
#include <ostream>
template <class L>//error one
class LinkedList
{
private:
Link<L> *pm_head;
Link<L> * pm_tail;
int m_numOfElements;
Link<L>* FindLink(L * dataToFind);
public:
LinkedList();
~LinkedList();
int GetNumOfElements(){return m_numOfElements;}
bool Add( L * data);
L *FindData(L * data);
template <class L> friend ostream & operator<<(ostream& os,const LinkedList<L> listToprint);//error two
L* GetDataOnTop();
bool RemoveFromHead();
L* Remove(L * toRemove);
this templete uses the link class templete
此模板使用链接类模板
#ifndef _Link_H_
#define _Link_H_
template <class T>//error 3
class Link
{
private:
T* m_data;
Link* m_next;
Link* m_prev;
public:
Link(T* data);
~Link(void);
bool Link::operator ==(const Link& other)const;
/*getters*/
Link* GetNext()const {return m_next;}
Link* GetPrev()const {return m_prev;}
T* GetData()const {return m_data;}
//setters
void SetNext(Link* next) {m_next = next;}
void SetPrev(Link* prev) {m_prev = prev;}
void SetData(T* data) {m_data = data;}
};
error one: shadows template parm `class L'
error two:declaration of `class L'
error three: shadows template parm `class T'
i dont understand what is the problem. i can really use your help thank you :)
我不明白是什么问题。我真的可以使用你的帮助谢谢:)
回答by Martin v. L?wis
These error messages really belong together:
这些错误消息确实属于一起:
a.cc:41: error: declaration of ‘class L'
a.cc:26: error: shadows template parm ‘class L'
This means that in line 41, you introduce a template parameter L; in my copy, this refers to
这意味着在第 41 行,你引入了一个模板参数 L;在我的副本中,这是指
template <class L> friend ostream & operator<<(ostream& os,
const LinkedList<L> listToprint);//error two
And that declaration shadows the template parameter in line 26:
该声明隐藏了第 26 行中的模板参数:
template <class L>//error one
class LinkedList
You need to rename the template parameter in the friend declaration.
您需要重命名友元声明中的模板参数。
Edit: The relevant language specification is 14.6.1/7
编辑:相关语言规范为 14.6.1/7
A template-parameter shall not be redeclared within its scope (including nested scopes). A template-parameter shall not have the same name as the template name.
模板参数不得在其范围内(包括嵌套范围)重新声明。模板参数不应与模板名称具有相同的名称。
When you refer to L
in const LinkedList<L> listToprint
, it's not clear whether you mean the L of the friend or the L of the class. So write
当您提到L
in 时const LinkedList<L> listToprint
,不清楚您指的是朋友的 L 还是班级的 L。所以写
template <class L1> friend ostream & operator<<(ostream& os,
const LinkedList<L1> listToprint);
回答by nimrodm
Just remove the
只需删除
template <class L>
from the friend
member function declaration.
从friend
成员函数声明。
You also need to replace uses of ostream
with std::ostream
unless you have a using namespace std
somewhere in your code.
您还需要替换ostream
with 的使用,std::ostream
除非您using namespace std
的代码中有某个地方。
Otherwise, the code looks fine.
否则,代码看起来不错。