Question

I have a program in C++ that organizes a bunch of college courses I want to take. It does so by taking input from the console (with things like course code, description, etc.), organizing each course by major, then outputting it all to a nicely-formatted, easy-to-read HTML file. Later, I plan on thinning out the list with a lot of research.

I implement each course as an object, which is added to a list when I finish entering the info. When I'm finished with all the info, list::sort should sort each course by major and code (eg, CSE 380 comes after CSE 110, and both come before ECO 108). The formatting afterwards is easy.

To sort, I have to implement a simple function, because even though not doing so is technically valid, I get a weird-ass error, I guess due to no '<' operator for my Course class. My function looks like this;

bool courseCompare(Course course1, Course course2) { return course1.getCode() < course2.getCode(); }

Where the getCode() returns a small string that holds the course code in three-letter/digit format (like "AMS 401"). This is meant to facilitate alphabetical order, obviously.

I call the sort method like so;

all_the_courses.sort(courseCompare);

Where all_the_courses is a list.

However, whenever I used std::list, the program just stops. Doesn't crash, gives no output, just sits there not responding when I input anything and hit Enter. Any ideas?

Était-ce utile?

La solution 2

OK, it seems like I was going about this the completely wrong way. Turns out my problem is something completely different (involving filestreams). I need to figure out what it is, but it's NOT the sorting; a simple cout (which I really should have used before) reveals that. Sorry, everyone!

Autres conseils

You are passing the parameters to your comparison function by value, not reference, so it's possible that the error lies in your copy constructor. The usual way to write a comparison function is with const references which avoids copying the object altogether:

bool courseCompare(const Course &course1, const Course &course2)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top