473,289 Members | 1,917 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,289 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 2653
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.