In need of a little help with a template class

Hi!
I'm trying to make a template class, called SortedList. The name actually says everything about what I want to do with it. So most of it is already working, but i have a little problem with a constructor. Here is my code (this is just the header file, the main doesnt matter now):

#ifndef S1_H_INCLUDED
#define S1_H_INCLUDED

#include <list>
#include <algorithm>
#include <functional>

template<class T, class Comp=std::less<T> >
class SortedList
{
std::list<T> li;
const Comp c;
public:
void insert (const T & t)
{li.insert(std::lower_bound(li.begin(),li.end(),t,c),t);}
int size() const {return li.size();}
const T & front() const {return li.front();}
const T & back() const {return li.back();}
void remove (const T & t) {li.remove(t);}
typedef typename std::list<T>::const_iterator const_iterator;
const_iterator begin() const {return li.begin();}
const_iterator end() const {return li.end();}
template<class Iterator>
SortedList (Iterator first, Iterator last): li(first, last)
{ li.sort(c);}
SortedList(){}
};

#endif // S1_H_INCLUDED

So my problem is that everything works, except the last line. When I write "SortedList<int> li;" in my main, it doesn't work, and I have no idea how to make it work. Supposedly it would use the last line, the empty constructor, but it keeps telling me that I should initialize my constant, but I have no idea how.
Any help is welcome, thank you guys in advance:)
Last edited on
closed account (S6k9GNh0)
What do you mean by "it doesn't work"? Is an error given?
Yes, here's the error:


v\c++\proba\proba\s1.h||In constructor 'SortedList<T, Comp>::SortedList() [with T = int, Comp = std::less<int>]':|
v\c++\proba\proba\main.cpp|21|instantiated from here|
v\c++\proba\proba\s1.h|26|error: uninitialized member 'SortedList<int, std::less<int> >::c' with 'const' type 'const std::less<int>'|
v\c++\proba\proba\s1.h||In constructor 'SortedList<T, Comp>::SortedList() [with T = double, Comp = std::less<double>]':|
v\c++\proba\proba\main.cpp|22|instantiated from here|
v\c++\proba\proba\s1.h|26|error: uninitialized member 'SortedList<double, std::less<double> >::c' with 'const' type 'const std::less<double>'|
||=== Build finished: 2 errors, 0 warnings ===|
You have a constant (const Comp) inside your class, but you never initialize it to anything in your constructor. That's bad.

Are you sure you need a copy of "Comp" in your class? I haven't really looked at the STL though, so I dunno if that's what they do.
You were right, I didn't need that c int the class, i just deleted that line, and wrote Comp(), where there was c before and it works. Thank you!!!!
Topic archived. No new replies allowed.