-2

First off, I want to say that I know this issue has come up before on StackOverflow. However, I am still having an issue with my code.

Roster* classRoster = new Roster(numStudents); 
cout << "Parsing data and adding students" << endl;
for (int i = 0; i < numStudents; i++){
    classRoster->parseAdd(allStudentData[i]); 
}
cout << "All done.\n";
cout << "Displaying all student information:\n";
classRoster->printAll();

cout << "Removing A3:\n"; 
if (classRoster->remove("A3")) classRoster->printAll();
else cout << "Student ID not found!\n";

In this code, the classRoster in if (classRoster->remove("A3")) gets an error saying that the "expression must have a bool type (or be convertible to bool)" and I honestly have no idea why. The classRoster above is working perfectly fine in the lines above the problematic code.

My remove() function is a void remove(string) function that accepts strings, so I don't believe it's the "A3" causing it issues.

Does anyone have an idea as to why this would be happening?

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • 4
    If `remove` has a return type of `void` (i.e. no return type), how do you expect its use in an `if` statement? The `if` requires a `true`/`false` value (or something convertible to `bool`) – ChrisMM Jul 23 '20 at 19:54
  • 2
    Your `classroster->remove()` is defined as having no return value, but you try to test that return value with the `if` statement. `if` requires a boolean test (*if this condition is true then do that*). Your definition of `void remove(string)` clearly says it returns nothing - that's what `void` means. Your `remove()` function should return true if it removed a record and false if it does not. – Ken White Jul 23 '20 at 19:57
  • Recommended reading: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) TL;DR: `new` is trickier to use than it looks and is the cause of a lot of the problems you'll encounter as a C++ programmer. It is also easy to avoid. Consider it an advanced C++ programming topic. – user4581301 Jul 23 '20 at 19:59
  • `Roster* classRoster = new Roster(numStudents);` --> `auto classRoster = std::make_unique(numStudents);` – Jesper Juhl Jul 23 '20 at 20:04
  • Thank you all for your help! So, should I rewrite "void remove" as a "bool remove" in order to get it to function properly? Or is there a way to make it function with "void remove"? My ultimate goal is for the program to find that ID and remove it. –  Jul 23 '20 at 20:06
  • @DuckyMomo a function that returns `void` explicitly returns *nothing*, so of course you cannot have an `if` statement that tests if it returns `true` or `false`. – Jesper Juhl Jul 23 '20 at 20:08
  • @Jesper Juhl thank you for your help as well. I'm quite new to C++ still and didn't even realize it! –  Jul 23 '20 at 20:11

1 Answers1

1

Make your remove member function return a bool value. Let it be 'bool remove(string)' instead of void.

Right now 'classRoster->remove("A3")' dosent return any value. Make it return something and you will fine.

Raz Crimson
  • 177
  • 9
  • I'm unable to upvote at the moment, but if I could, I would! Thank you very much for explaining that and for your help :) –  Jul 23 '20 at 20:12