on the line where I am trying to do this sort algorithm sort(tag_info.begin(), t
ID: 3794805 • Letter: O
Question
on the line where I am trying to do this sort algorithm
sort(tag_info.begin(), tag_info.end(), compareFirst);
I keep getting this error reference to non-static member function must be called I have tried many things and it is still giving me that error. Can someone give me an explanation or any kind of advice would be appriteated.(I am using my own vector class and I belive the issue is in compareFirst part)
The code below bool gameCenter:: compareFirst( pair<int,String>&i, pair<int,String>&j)
{ return i.first < j.first;
}
bool gameCenter:: compareSecond(pair<int,String>&i, pair<int,String>&j)
{ return i.second < j.second;
}
//score calculates the individual scores of players and number of tags
void gameCenter::Score(){ for(int i=0; i<num_teamA_players; i++){ cout<<teamA.player[i].player_name<<endl;
}
for(int i=0; i<num_teamB_players; i++){ cout<<teamB.player[i].player_name<<endl;
}
//loop one iterates over all of the tags from match file
for(int j=0; j<num_of_tags; j++){ //loop two iterates over all of the team A playesrs
for(int i=0; i<num_teamA_players; i++){ //if statment to determine is the tagger is in team A
if(tagger_id[j]==teamA.player[i].id){ //when there is a match 1 is added to no. of tags
teamA.player[i].tag+=1;
//the player score is calculated
teamA.player[i].score+=tag_location(location_id[j]);
}
}
for (int k=0; k<num_teamB_players; k++){ if(target_id[j]==teamB.player[k].id){ teamA.player[k].indv_tag_count+=1;
}
}
//the same exact syntax as A is performed for team B
for (int i=0; i<num_teamB_players; i++){ if(tagger_id[j]==teamB.player[i].id){ teamB.player[i].tag+=1;
teamB.player[i].score+=tag_location(location_id[j]);
teamB.player[i].tagged=target_id[j];
}
}
}
for(int i=0; i<num_teamA_players; i++){ tag_pair.first=teamA.player[i].player_name;
tag_pair.second=teamA.player[i].tag;
tag_info.add(tag_pair);
}
//Here is the error
sort(tag_info.begin(), tag_info.end(), compareFirst);
for(int i=0; i<num_teamA_players; i++){ cout<<tag_info[i].first<<" "<<tag_info[i].second<<endl;
}
//once score is callculated function best score is called
bestScore();
}
Explanation / Answer
Hi, Please find my explanation.
Please let me know in case of any issue.
//Here is the error
sort(tag_info.begin(), tag_info.end(), compareFirst);
Error: reference to non-static member function must be called
This error will come because I think your 'sort' method is 'static'(class level) function for your class.
Just make it not static
'compareFirst' method is a member function of your class. It is called outside or inside a static method,
then it should be called using object. You can not call it directly
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.