Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template class c++

Tags:

c++

templates

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

like image 289
inna karpasas Avatar asked Jan 01 '11 11:01

inna karpasas


People also ask

Is template available in C?

C does not have static templates, but you can use macros to emulate them.

What is meant by template in C?

A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

What is a template class C++?

Definition. As per the standard definition, a template class in C++ is a class that allows the programmer to operate with generic data types. This allows the class to be used on many different data types as per the requirements without the need of being re-written for each type.

What is template and example?

A template is a form, mold or pattern used as a guide to make something. Here are some examples of templates: Website design. Creating a document. Knitting a sweater.


1 Answers

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);
like image 54
Martin v. Löwis Avatar answered Sep 19 '22 16:09

Martin v. Löwis