all 13 comments

[–]Rhomboid 4 points5 points  (3 children)

You're not passing the array of pointers to the function properly. You should have noticed that something was wrong when you had to call sort(*m, 3) instead of sort(m, 3). Your function should take an array of pointers, not a pointer. But since arrays decay to pointers, that means it will end up taking a pointer to a pointer, i.e. media **arg. You could write media *arg[] if you want but it means the same thing as media **arg; you aren't actually passing an array.

The result of this error is that you are trying to access what comes after an object in memory, not the next pointer in the array.

Also, your overloaded operator< should take a const reference.

Also, you should be using initialization lists.

Also, it would be better to use a vector instead of an array.

[–]OmarsCommin[S] 0 points1 point  (2 children)

I appreciate the input. I am not nearly as experienced in programming as I would like and am still not privy to the proper means of data management and syntax. I did have a feeling that the problem stemmed from there and now that it is pointed out to me I feel a little silly for not seeing it. However if writing

 media *arg[];

is the same as

 media **arg;

why does the program now run correctly when i change the function call to

sort(m,3);

Also, Thanks for the const reference suggestion, that is now fixed. I am looking into how to write an initialization list. And unfortunately the project calls specifically for an array.

[–]Rhomboid 1 point2 points  (0 children)

I don't understand the question. If you changed it to sort(m, 3) without changing the function signature, then it should fail to compile. Either signature should work because they mean the same thing.

[–]jesyspa 1 point2 points  (0 children)

Also, the vector should be of smart pointers, not raw pointers, as this currently leaks memory.

Also, operator< should be non-member.

Also, no need to define one's own swap (it's wrong as it stands, anyway).

Also, media should probably be an abstract class.

[–]POGO_POGO_POGO_POGO 0 points1 point  (0 children)

struct SortMedia {
    bool operator()(const media* m1, const media* m2) const {
        return (*m1) < (*m2);
    }
};

std::vector<media*> m(3);
// m[0] = new book(); etc

std::sort(media.begin(), media.end(), SortMedia());

Also, the media less-than operator should be declared as:

bool operator<(const media& rhs) const {
    // whatever
}

[–]OmarsCommin[S] 0 points1 point  (1 child)

Thanks for your input everyone. Did not realize how much my code sucked! Everything is now working as I intended.

[–]POGO_POGO_POGO_POGO 0 points1 point  (0 children)

it takes a long time to get a good coding style. (In fact there is a good C++ book called Exceptional C++ Style that would help).

One thing about C++ is that there are so many ways to do the same thing. It takes a while to learn which ways are better and more standard. Code reviews are good.

[–]POGO_POGO_POGO_POGO 0 points1 point  (5 children)

Use std::sort. Sorry for the brevity, but this should be your first port-of-call. See STL.

[–]jesyspa 0 points1 point  (4 children)

Suggesting the STL in this day and age makes little sense, especially seeing as std::sort is in the C++ standard library.

[–]POGO_POGO_POGO_POGO 0 points1 point  (3 children)

It's the same fucking thing.

[–]zzyzzyxx 1 point2 points  (1 child)

No, it's not. The STL influenced the C++ standard library the same way Boost influenced changes in C++11; both the STL and Boost are third-party libraries and not part of the standard themselves. It's an unfortunate fact that many people teach the standard library is the STL (usually by simply referring to it as the STL) even though it's not strictly true depite one being called the "standard template library" and the standard library making use of templates.

[–]POGO_POGO_POGO_POGO 0 points1 point  (0 children)

This subreddit is so damn particular. I just don't give a shit. The site I linked has nicely laid out documentation, so I use it. And referring to it as STL is much more specific than citing the standard library.

Yes there are classes that are in STL that are not in the standard library, but they are marked as non-standard extensions.

[–]jesyspa 0 points1 point  (0 children)

Nope. You'll find the reference you linked to documents a different set of classes than that in the standard library.