[英]template class c++
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 这个模板使用链接类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 :) 我真的可以用你的帮助谢谢:)
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; 这意味着在第41行中,您引入了模板参数L; in my copy, this refers to 在我的副本中,这指的是
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. 您需要在friend声明中重命名模板参数。
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. 当你在const LinkedList<L> listToprint
引用L
时,不清楚你是指朋友的L还是班级的L。 So write 所以写
template <class L1> friend ostream & operator<<(ostream& os,
const LinkedList<L1> listToprint);
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. 您还需要使用std::ostream
替换ostream
使用,除非您在代码中的某处using namespace std
。
Otherwise, the code looks fine. 否则,代码看起来很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.