std::list wrapper (+templates)

I

isliguezze

template <class T>
class List {
public:
List();
List(const List&);
List(int, const T&);

void push_back(const T &);
void push_front(const T &);
void pop_back();
void pop_front();
void remove(const T &);
int size();

friend std::eek:stream &operator<<(std::eek:stream &out, const List<T> &);
private:
std::list<T> *lst;
};

template <class T>
List<T>::List() { lst = new std::list<T>(); }

template <class T>
List<T>::List(const List &rhs) { lst = new std::list<T>(rhs.lst); }

template <class T>
List<T>::List(int n, const T& value) { lst = new std::list<T>(n,
value); }

template <class T>
void List<T>::push_back(const T& value) { lst->push_back(value); }

template <class T>
void List<T>::push_front(const T& value) { lst->push_front(value); }

template <class T>
void List<T>::pop_back() { lst->pop_back; }

template <class T>
void List<T>::pop_front() { lst->pop_front; }

template <class T>
void List<T>::remove(const T& value) { lst->remove(value); }

template <class T>
int List<T>::size() { return (int)lst->size; }

template <class T>
std::eek:stream &operator<<(std::eek:stream &out, const List<T> &L)
{
for (std::list<T>::iterator it = L.lst->begin(); it != L.lst->end(); +
+it)
out << " " << *it;

out << std::endl;
return out;
}

int main()
{
List<int> il;

for (int i = 0; i < 10; ++i)
il.push_back(i);

std::cout << il;

return 0;
}



There's a crazy problem with operator>> MSVS6 says that 'lst' is
undeclared... I'm astounded... GNU G++ says that 'it' (the iterator in
operator>>) is not declared in this scope..
 
I

isliguezze

Thank you very much for 'for (typename ...)' . But there is still a
question about operator<<. You've said that this declaration introduce
a NON-template operator<< function into the circulation, but how to
use operator>> with templates? Is it possible anyhow?
 
I

isliguezze

GNU G++ says: undefined reference to operator<<(std::eek:stream &,
List<int> const&);

One more question: why <int>?
 
F

Frank Birbacher

Hi!

Victor is right about the "declaration" problem: the friend declares a
non-template operator >>, but you only provide for a templated operator
friend std::eek:stream &operator<<(std::eek:stream &out, const List<T> &);

In short: Don't use a friend at all.

There is no need to. Instead implement a member function "void
printTo(std::eek:stream&) const" that does the job. Then define
"operator>>" to call "printTo". Look ma', no friends. :)
template <class T>
std::eek:stream &operator<<(std::eek:stream &out, const List<T> &L)
{
L.printTo(out);
return out;

By the way, your class is missing an operator = which needs to copy your
pointer.

Regards,
Frank
 
I

isliguezze

Thanks, Frank, 'printTo' works OK. But I solved the problem of friend
operator:

template <class T> class List;

template <class U> std::eek:stream& operator<< (std::eek:stream &, const
List<U> &);

template <class T>
class List {
public:
// ...
template <class U>
friend std::eek:stream &operator<<(std::eek:stream &, const List<U> &);
private:
std::list<T> *lst;
};

// ...
template <class U>
std::eek:stream &operator<<(std::eek:stream &out, const List<U> &L)
{
for (typename std::list<U>::iterator it = L.lst->begin(); it !=
L.lst->end(); ++it)
out << " " << *it;

out << std::endl;
return out;
}

int main()
{
List<int> il;

for (int i = 0; i < 10; ++i)
il.push_back(i);

std::cout << il;

return 0;
}
 
I

isliguezze

Thanks, Frank, 'printTo' works OK. But I solved the problem of friend
operator:

template <class T> class List;

template <class U> std::eek:stream& operator<< (std::eek:stream &, const
List<U> &);

template <class T>
class List {
public:
// ...
template <class U>
friend std::eek:stream &operator<<(std::eek:stream &, const List<U> &);
private:
std::list<T> *lst;
};

// ...
template <class U>
std::eek:stream &operator<<(std::eek:stream &out, const List<U> &L)
{
for (typename std::list<U>::iterator it = L.lst->begin(); it !=
L.lst->end(); ++it)
out << " " << *it;

out << std::endl;
return out;
}

int main()
{
List<int> il;

for (int i = 0; i < 10; ++i)
il.push_back(i);

std::cout << il;

return 0;
}
 
I

isliguezze

Hey, gentlemen, it's interesting, but there's one more equivalent form
of doing this:
using namespace std;

template <class T> class List;
template <class U> ostream& operator<< (ostream &, const List<U> &);

template <class T> class List {
public: //...
friend ostream &operator<< <T> (ostream &, const List<T>
&); //!!!
private:
std::list<T> *lst;
};
//...
template <class T> ostream& operator<<(ostream &out, const List<T> &L)
{
for (typename list said:
end(); ++it)
out << " " << *it;

out << std::endl;
return out;
}
//...
 
F

Frank Birbacher

Hi!

Hey, gentlemen, it's interesting, but there's one more equivalent form
of doing this: [snip]
friend ostream &operator<< <T> (ostream &, const List<T>
&); //!!!

Yes. That is the declaration of a templated method. And yes, you need to
first declare the whole function (the "template<typename T> ostream&
operator << (ostream&, const List<T>&);" line).

And this friend declaration is just the minimal friend declaration you
need. Your previous post made all instances of the template method a friend.

Regards, Frank
 
T

Triple-DES

List<T>::List() { lst = new std::list<T>(); }

It's a good idea to use an initializer list for this:
List said:
template <class T>
List<T>::List(const List &rhs) { lst = new std::list<T>(rhs.lst); }

This copy ctor is ill-formed (as you will quickly notice if you try to
use it). Also, prefer to use an initializer list here too.
template <class T>
int List<T>::size() { return (int)lst->size; }

It may be a good idea to use an unsigned type here (size_t comes to
mind), since all the standard containers use this (Just a suggestion).

DP
CAPTCHA: inact
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top