Question
Someone answered my question here in chegg with this code. But it is giving me a lot of errors related with the use of an
Someone answered my question here in chegg with this code. But it is giving me a lot of errors related with the use of an undeclared identifier
use of undeclared identifier 'defender_queue'
use of undeclared identifier 'hunter_queue'
use of undeclared identifier 'bard_queue'
This is the code he gave me. You can only change lfgqueue.cpp file. Do not change the main code, player.cpp file, lfgqueue.h file and player.h
---------lfgqueue.cpp--------
#include "lfgqueue.h"
#include
// Adds a player to the queue
void LFGQueue::push_player(Player* player) {
// Add the player to the corresponding role queue
switch(player->role()) {
case Player::Defender:
defender_queue.push(player);
break;
case Player::Hunter:
hunter_queue.push(player);
break;
case Player::Bard:
bard_queue.push(player);
break;
}
// Try to form a group if possible
while(defender_queue.size() > 0 && hunter_queue.size() > 0 && bard_queue.size() > 0) {
// Remove one player of each role from their respective queues
Player* defender = defender_queue.front();
defender_queue.pop();
Player* hunter = hunter_queue.front();
hunter_queue.pop();
Player* bard = bard_queue.front();
bard_queue.pop();
// Add the players to the group queue
group_queue.push(defender);
group_queue.push(hunter);
group_queue.push(bard);
}
}
// Removes a player of a given role from the queue
void LFGQueue::pop_player(Player::Role role) {
// Remove the player from the corresponding role queue
switch(role) {
case Player::Defender:
if(defender_queue.size() == 0) {
throw std::out_of_range("No defenders in queue.");
}
defender_queue.pop();
break;
case Player::Hunter:
if(hunter_queue.size() == 0) {
throw std::out_of_range("No hunters in queue.");
}
hunter_queue.pop();
break;
case Player::Bard:
if(bard_queue.size() == 0) {
throw std::out_of_range("No bards in queue.");
}
bard_queue.pop();
break;
}
}
// Returns the number of players in the queue
int LFGQueue::size() const {
return defender_queue.size() + hunter_queue.size() + bard_queue.size();
}
// Returns the front player of a given role in the queue
Player* LFGQueue::front_player(Player::Role role) const {
// Return the front player of the corresponding role queue
switch(role) {
case Player::Defender:
if(defender_queue.size() == 0) {
return 0;
}
return defender_queue.front();
case Player::Hunter:
if(hunter_queue.size() == 0) {
return 0;
}
return hunter_queue.front();
case Player::Bard:
if(bard_queue.size() == 0) {
return 0;
}
return bard_queue.front();
}
}
-------------main.cpp-----------
#include#include #include #include "lfgqueue.h" using namespace std; inline void _test(const char* expression, const char* file, int line) { cerr << "test(" << expression << ") failed in file " << file; cerr << ", line " << line << "." << endl; abort(); } #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__)) int main() { // Variables used for testing Player* group[3]; Player daria("Daria", Player::Defender); Player daniela("Daniela", Player::Defender); Player hector("Hector", Player::Hunter); Player hugo("Hugo", Player::Hunter); Player berta("Berta", Player::Bard); Player bernardo("Bernardo", Player::Bard); // Test size(), add(), front_player(), pop_player() // on a small example. LFGQueue q; test(q.size() == 0); test(q.front_player(Player::Defender) == 0); test(q.front_player(Player::Hunter) == 0); test(q.front_player(Player::Bard) == 0); q.push_player(&daniela); test(q.size() == 1); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == 0); test(q.front_player(Player::Bard) == 0); q.push_player(&hector); test(q.size() == 2); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == 0); q.push_player(&berta); test(q.size() == 3); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &berta); q.push_player(&hugo); test(q.size() == 4); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &berta); q.push_player(&bernardo); test(q.size() == 5); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &berta); q.push_player(&daria); test(q.size() == 6); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &berta); // Order is now [Daniela, Hector, Berta, Hugo, Bernardo, Daria] q.pop_player(Player::Defender); test(q.size() == 5); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &berta); q.pop_player(Player::Hunter); test(q.size() == 4); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); q.pop_player(Player::Bard); test(q.size() == 3); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &bernardo); q.pop_player(Player::Bard); test(q.size() == 2); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == 0); q.pop_player(Player::Defender); test(q.size() == 1); test(q.front_player(Player::Defender) == 0); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == 0); q.pop_player(Player::Hunter); test(q.size() == 0); test(q.front_player(Player::Defender) == 0); test(q.front_player(Player::Hunter) == 0); test(q.front_player(Player::Bard) == 0); // Test previous methods plus front_group(), pop_group() on // a small example. q.push_player(&hugo); test(q.size() == 1); test(q.front_player(Player::Defender) == 0); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == 0); test(!q.front_group(group)); q.push_player(&hector); test(q.size() == 2); test(q.front_player(Player::Defender) == 0); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == 0); test(!q.front_group(group)); q.push_player(&berta); test(q.size() == 3); test(q.front_player(Player::Defender) == 0); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); test(!q.front_group(group)); q.push_player(&bernardo); test(q.size() == 4); test(q.front_player(Player::Defender) == 0); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); test(!q.front_group(group)); q.push_player(&daria); test(q.size() == 5); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); test(q.front_group(group)); q.push_player(&daniela); test(q.size() == 6); test(q.front_player(Player::Defender) == &daria); test(q.front_player(Player::Hunter) == &hugo); test(q.front_player(Player::Bard) == &berta); test(q.front_group(group)); // Order is now [Hugo, Hector, Berta, Bernardo, Daria, Daniela] group[0] = group[1] = group[2] = 0; test(q.front_group(group)); test(group[0] == &daria); test(group[1] == &hugo); test(group[2] == &berta); q.pop_group(); test(q.size() == 3); test(q.front_player(Player::Defender) == &daniela); test(q.front_player(Player::Hunter) == &hector); test(q.front_player(Player::Bard) == &bernardo); group[0] = group[1] = group[2] = 0; test(q.front_group(group)); test(group[0] == &daniela); test(group[1] == &hector); test(group[2] == &bernardo); q.pop_group(); test(q.size() == 0); test(q.front_player(Player::Defender) == 0); test(q.front_player(Player::Hunter) == 0); test(q.front_player(Player::Bard) == 0); // Test a set of 999 players: 333 of each role in the following order: // 333 defenders, 1 hunter, 1 bard, 1 hunter, 1 bard... // Create the players Player** players = new Player*[999]; ostringstream oss; for (int i = 0; i < 333; ++i) { oss.str(""); oss << "Defender " << i+1; players[i] = new Player(oss.str(), Player::Defender); } for (int i = 333; i < 999; i+=2) { oss.str(""); oss << "Hunter " << (i-333)/2+1; players[i] = new Player(oss.str(), Player::Hunter); } for (int i = 334; i < 999; i+=2) { oss.str(""); oss << "Bard " << (i-334)/2+1; players[i] = new Player(oss.str(), Player::Bard); } // Add them to the queue for (int i = 0; i < 999; ++i) q.push_player(players[i]); test(q.size() == 999); // Repeatedly check and remove the frontmost players. // Because of how we scrambled, this has a fixed order. for (int i = 0; i < 333; ++i) { group[0] = group[1] = group[2] = 0; test(q.front_group(group)); oss.str(""); oss << "Defender " << i+1; test(group[0]->name() == oss.str()); oss.str(""); oss << "Hunter " << i+1; test(group[1]->name() == oss.str()); oss.str(""); oss << "Bard " << i+1; test(group[2]->name() == oss.str()); q.pop_group(); test(q.size() == 999 - 3 * (i+1)); } test(q.size() == 0); test(!q.front_group(group)); for (int i = 0; i < 999; ++i) delete players[i]; delete[] players; cout << "Assignment complete." << endl; }
player.h
#ifndef PLAYER_H #define PLAYER_H #includeusing namespace std; class Player { public: // This works like a custom type with just four values. // Outside of Player methods, reference them like: // "if (p->role == Player::Defender)", etc. enum Role {Defender, Hunter, Bard}; // Initializes a player with the given name and role Player(string name, Role role); // Returns the name of the player string name(); // Returns the role of the player Role role(); private: string _name; Role _role; }; #endif
player.cpp
#include "player.h" Player :: Player(string name, Role role) { _name = name; _role = role; } string Player :: name() { return _name; } Player::Role Player :: role() { return _role; }
lfgqueue.h
#ifndef LFGQUEUE_H #define LFGQUEUE_H #include "player.h" class LFGQueue { public: // Constructs a new empty queue LFGQueue(); // Returns the number of players in the queue. int size(); // Adds a (pointer to a) player to the back of the queue void push_player(Player* p); // Returns a pointer to the frontmost player // with the specified role. If no such player // exists, returns 0. Player* front_player(Player::Role r); // Removes the frontmost player with the // specified role. If no such player exists // does nothing. void pop_player(Player::Role r); // Returns whether the queue contains a complete group // (a Defender, a Hunter, and a Bard). // // If the queue contains a complete group, the method // sets the first three elements of the array parameter // equal to the addresses of the frontmost: // 1. Defender (index 0) // 2. Hunter (index 1) // 3. Bard (index 2) bool front_group(Player** group); // Removes the frontmost Defender, Hunter, // and Bard from the queue. If some role // has no player with that role, then // no players are removed. void pop_group(); private: Player** players; int count; int capacity; };
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started