473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

std::list wrapper (+templates)

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::ostream &operator<<(std::ostream &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::ostream &operator<<(std::ostream &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<intil;

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..
Jun 27 '08 #1
12 2663
is********@gmail.com wrote:
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::ostream &operator<<(std::ostream &out, const List<T&);
I am not sure (friend declarations inside templates always confuse me)
but doesn't this declaration introduce a NON-template operator<<
function into the circulation?
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::ostream &operator<<(std::ostream &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<intil;

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..
Well, MSVC6 is notoriously bad when it comes to templates. Download and
use their latest (2008) version, Express Edition. As for G++, it's
completely correct, 'std::list<T>::iterator' is not a type, you have to
tell the compiler that it's a type by means of 'typename' keyword:

for( typename std::list<T>::iterator it ...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
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?
Jun 27 '08 #3
GNU G++ says: undefined reference to operator<<(std::ostream &,
List<intconst&);

One more question: why <int>?
Jun 27 '08 #4
is********@gmail.com wrote:
GNU G++ says: undefined reference to operator<<(std::ostream &,
List<intconst&);
That's right, I think you've stumbled upon the rule where if you declare
a straight-forward 'friend' in a class template, it declares a
non-template function as the friend, and now wants you to provide that
function (the declaration is there, but it cannot make the template the
definition for a non-template function declared in the 'friend'
declaration). Different compilers do different things in these
circumstances. And I am not sure how to straighten it out, sorry. Try
looking in the archives for 'template friend operator'...
One more question: why <int>?
Because your 'il' variable is of that type, isn't it?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #5
Hi!

Victor is right about the "declaration" problem: the friend declares a
non-template operator >>, but you only provide for a templated operator
>>. That's why the linker rejects the code.
is********@gmail.com schrieb:
friend std::ostream &operator<<(std::ostream &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::ostream&) const" that does the job. Then define
"operator>>" to call "printTo". Look ma', no friends. :)
template <class T>
std::ostream &operator<<(std::ostream &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
Jun 27 '08 #6
Thanks, Frank, 'printTo' works OK. But I solved the problem of friend
operator:

template <class Tclass List;

template <class Ustd::ostream& operator<< (std::ostream &, const
List<U&);

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

// ...
template <class U>
std::ostream &operator<<(std::ostream &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<intil;

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

std::cout << il;

return 0;
}
Jun 27 '08 #7
Thanks, Frank, 'printTo' works OK. But I solved the problem of friend
operator:

template <class Tclass List;

template <class Ustd::ostream& operator<< (std::ostream &, const
List<U&);

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

// ...
template <class U>
std::ostream &operator<<(std::ostream &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<intil;

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

std::cout << il;

return 0;
}
Jun 27 '08 #8
Hey, gentlemen, it's interesting, but there's one more equivalent form
of doing this:
using namespace std;

template <class Tclass List;
template <class Uostream& operator<< (ostream &, const List<U&);

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

out << std::endl;
return out;
}
//...
Jun 27 '08 #9
Hi!

is********@gmail.com schrieb:
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 Tostream&
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
Jun 27 '08 #10

<is********@gmail.comwrote in message
news:31**********************************@e39g2000 hsf.googlegroups.com...
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::ostream &operator<<(std::ostream &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); }
[...]

Where are you deleting the above allocation? I see no dtor in List<T>...

Jun 27 '08 #11
On 21 Mai, 21:45, "isligue...@gmail.com" <isligue...@gmail.comwrote:
[snip]
List<T>::List() { lst = new std::list<T>(); }
It's a good idea to use an initializer list for this:
List<T>::List() : lst(new std::list<T>) {}
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
Jun 27 '08 #12
On 2008-05-23 02:03:23 -0400, Triple-DES <De**********@gmail.comsaid:
On 21 Mai, 21:45, "isligue...@gmail.com" <isligue...@gmail.comwrote:
[snip]
>List<T>::List() { lst = new std::list<T>(); }

It's a good idea to use an initializer list for this:
List<T>::List() : lst(new std::list<T>) {}
>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).
Even better would be std::list<T>::size_type.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sebastian Faust | last post by:
Hi, I am thinking now for a while about a design decision. I would be glad if you can give me some advices which is maybe the better way for solving my problem. I wanna do something like...
2
by: Shea Martin | last post by:
Solaris 9, Sun Workshop 5.0 I have ObjectA.o, ObjectB.o. ObjectA.o uses std::list. I created a archive of the objects: ar cvr libAB.a *.o. I have example.cc which I am trying to compile...
1
by: Glen Able | last post by:
Hi, I have a collection of lists, something like this: std::list<Obj*> lists; Now I add an Obj to one of the lists: Obj* gary; lists.push_front(gary);
15
by: sandwich_eater | last post by:
I want to know how to set an std::list iterator variable to make it null or nil. If this is not possible what is the value of an uninitialised std::list iterator and is it ok to assign this value...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
2
by: ranin02 | last post by:
Hi, We have a list derived from std::list that has a custom allocator derived from std::allocator. This was originally written using VC++ 6.0 which required a workaround for the fact that 6.0...
19
by: Juha Nieminen | last post by:
If I'm not completely mistaken, the only reason why std::list::size() may be (and usually is) a linear-time operation is because they want std::list::splice() to be a constant-time operation, and...
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.