模板类 c++

发布于 2024-10-10 13:01:17 字数 1363 浏览 0 评论 0原文

我尝试为我的大学项目设计一个模板。我编写了以下代码:

#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);

此模板使用链接类模板

#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 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花期渐远 2024-10-17 13:01:17

这些错误消息确实属于一起:

a.cc:41: error: declaration of ‘class L’
a.cc:26: error:  shadows template parm ‘class L’

这意味着在第 41 行中,您引入了一个模板参数 L;在我的副本中, this 指的是

template <class L> friend ostream & operator<<(ostream& os,
               const LinkedList<L> listToprint);//error two

并且该声明遮盖了第 26 行中的模板参数:

template <class L>//error one
class LinkedList

您需要在友元声明中重命名模板参数。

编辑:相关语言规范为14.6.1/7

模板参数不得
在其范围内重新声明(包括
嵌套范围)。模板参数
不得与该人同名
模板名称。

当您在 const LinkedList中引用 L 时, listToprint,不清楚你指的是朋友的L还是班级的L。所以写

template <class L1> friend ostream & operator<<(ostream& os,
    const LinkedList<L1> listToprint);

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

template <class L> friend ostream & operator<<(ostream& os,
               const LinkedList<L> listToprint);//error two

And that declaration shadows the template parameter in line 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

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

template <class L1> friend ostream & operator<<(ostream& os,
    const LinkedList<L1> listToprint);
债姬 2024-10-17 13:01:17

删除 即可

 template <class L>

只需从 friend 成员函数声明中

。您还需要将 ostream 的使用替换为 std::ostream ,除非您的代码中某处有 using namespace std

否则,代码看起来不错。

Just remove the

 template <class L>

from the friend member function declaration.

You also need to replace uses of ostream with std::ostream unless you have a using namespace std somewhere in your code.

Otherwise, the code looks fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文