I am wondering how would I deal with a call to a function when an integer is passed into a function that accepts a pointer? In my case hasPlayedInTeam() accepts a pointer to Team, however, received an int. This causes the Q_ASSERT to hang.
In addition, is my problem also known as a null pointer? My professor has used that term several times in lecture, but I am not sure what he was referring to.
//main.cpp
Person p1("Jack", 22, "UCLA");
Q_ASSERT(p1.hasPlayedInTeam(0) == false);
//person.cpp
bool Person::hasPlayedInTeam(Team *pTeam) {
bool temp = false;
foreach (Team* team, teamList) {
if (team->getName() == pTeam->getName() {
temp = true;
}
}
return temp;
}
In your call:
the integer literal
0is converted to aNULLpointer. So, you are not actually “receiving” an integer; you are passing an integer, the compiler can automatically cast it to the null pointer (given the definition for NULL).I think you can fix the definition of
hasPlayedInTeamby either asserting that its argument is not NULL, or by returning a default value when NULL is passed in:or: