Using STL List to create a "sorted bag"

Hey guys, I'm new around here. I'm a student at U of Colorado and I am studying Computer Science. I am currently in a data structures class that I enjoy quite a bit. I am working on a project that uses the list container class from STL to store items (int, char, double, etc.) in a sorted bag. Although the list class provides a sort function, I am responsible for sorting the items manually as they go in the bag. I am having quite a bit of trouble getting this thing to compile. Here is what i have:

sorted_bag.h:
http://codepad.org/MbHbqo6o

sorted_bag.cxx:
http://codepad.org/eomsplD6

Here are my problems. First, I have no idea how to write the += and = operators so that they work properly. I keep getting quite a few errors on this. Also, the IDE I'm using (Xcode on a mac) is complaining about my friend function declarations being non-template functions (well, no crap, but why is this an issue?). If you wouldn't mind looking my code over, it would be greatly appreciated. Thanks everyone!
Last edited on
The compiler is complaining about those friend functions rightfully. See http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16 (hint: it is operator<< <> ( )

Also, those two constructors and the destructor are redundant and do more work than necessary, just don't provide them at all.

occurrences() should be const, and could be a one-liner if you could use the C++ library function std::count().

insert() could insert at std::upper_bound, also a one-liner in C++

You may want to replace
for(i = target.begin(); i != target.end(); i++)
with
for(i = target.bag.begin(); i != target.bag.end(); i++)
in that operator<< <> (otherwise why was it friended to begin with)
Last edited on
Topic archived. No new replies allowed.