0

Why this if is not evaluated? C++

I'm trying to sort objects either by song name or artist name. If one choose by song name, the setterFlag value is true, if not, the setterFlag is false. The problem that I have, is, that in the overload operator >, the if statement (where evaluates if the flag is true or false) it seems that doesn't work. the flag is always false, why it doesn't work? the flag variable it has its getters and setters. bool Songs::operator > (const Songs& s) const{ // it seems that this if doesn't work, if(obj.getFlag()) return songName > s.songName; else return singer > s.singer; }

2nd Mar 2019, 11:05 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
8 Answers
+ 2
An example is usually better. https://code.sololearn.com/cmQru5q137vB/?ref=app Because Menu and Song both need to know about each other I can't put them in a single file and it won't run on sololearn. :( Which is one of the reasons I would pick the 2nd option. Song wouldn't need to know about Menu there.
3rd Mar 2019, 8:30 PM
Dennis
Dennis - avatar
+ 2
Sorry, I went to bed :) In your code you have Menu obj in the songs.cpp and you use that object to get the flag from. But I can't seem to find code that calls showMenu on that object. Are you sure you are changing the flag in the correct object? There is no main function so it's just a random guess.
3rd Mar 2019, 8:46 AM
Dennis
Dennis - avatar
+ 1
I'm gonna need to see some more code to be able to judge what is going on here. On another note, I recommend using function pointers that point to a sorting function so that you can change functions on the fly instead. Here is a quick example: https://code.sololearn.com/cH67qrFMBkiq/?ref=app
2nd Mar 2019, 11:38 PM
Dennis
Dennis - avatar
+ 1
Well, the code is a little long, so, in a nutshell, here is the important parts that I considered. https://code.sololearn.com/c4iBdhuhcNJq I'm doing a homework about sorthing methods (the short methods works perfectly), I need to have an option to sort the song list by song name, or song artist. In the code that you have, sort by song name. Just changing songName by artist, and will sort by artist name. I'm overloading the operators because I'm using objects. My main idea was, to have a flag and give a value depending on what option you chose (by song name or song artist). True will be song name and false will be song artist. I set those values in the file Menu.cpp, line 315 and 336, and depending on that, activate songName or singer. But somehow, dunno why, in the block of the operators overloading, the if doesn't work and because of that, will sort by singer name. I don't know how to solve this problem or another solution...
3rd Mar 2019, 1:23 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
+ 1
Well, the main function only calls showMenu and that's all. Include <iostream> Include <Menu.h> Int main(){ Menu obj; Obj.showMenu(); } because showMenu is being called, it has to get the flag but, or where do I need to use setFlag()? I used it when the user choosed the option, but when I use getFlag() on the operators overloading, it seems that it doesn't get the value stored previously by setFlag()... but when I print getFlag() on the Menu.cpp, it shows me the value of the flag... Or maybe I'm using set and get flag in a wrong way and I can't see it ☹️
3rd Mar 2019, 5:43 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
+ 1
Yeah, the problem is that you have 2 Menu objects. 1: in main; The object where you call showMenu on and is therefore the object whose flags are manipulated. 2: The global one in songs.cpp; The object that your song functions access. But this object doesn't have its flags manipulated. Remove this one and fix all the errors you get. :) You could pass a (const) reference of the menu to each song, this would allow each song to access its parent( the menu ) and check the flags. Another option is to move all the sorting logic to the Menu instead, the menu would then use the song's getters to sort its list. Personally I would opt for the 2nd option. Lemme know if you need more help. :)
3rd Mar 2019, 7:03 PM
Dennis
Dennis - avatar
+ 1
I see, the global object in songs.cpp, is independent from the menu object right? that's why it doesn't have a value, I thought that creating a Menu object in songs.cpp I could access to the flag value. I didn't understand too much this part: "You could pass a (const) reference of the menu to each song, this would allow each song to access its parent( the menu ) and check the flags." Can you please explain me again or give me an example?
3rd Mar 2019, 7:37 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
+ 1
Dennis thank you so much! Is now more clear to me! Sorry if I didn't answer before, I was busy doing homework 😅
7th Mar 2019, 8:11 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar